0

I am having an issue where IE is not working with the following code. The code basically hides a div if elements are empty, you will notice it works in all but IE.

//hide webapp item if empty
if ($("#features").html().trim()) {} else {
    $("#f-h").hide()
}
if ($("#application").html().trim()) {} else {
    $("#a-h").hide()
}
if ($("#tech-specs").html().trim()) {} else {
    $("#t-h").hide()
}
if ($("#downloads").html().trim()) {} else {
    $("#d-h").hide()
}
if ($("#custom-field").html().trim()) {} else {
    $("#c-f").hide()
}

The pages to see it in action is webpage

Any hints to why ie doesnt like this or a better method would be appreciated.

5
  • 2
    Always write code iteratively. Step by step. If you followed this technique - you would know what exact piece of code caused an issue.
    – zerkms
    Commented Sep 14, 2011 at 3:04
  • 6
    This is pretty ugly. Instead of having an empty if/non-empty else, simply negate the if: if (!$("#features").html().trim()) { $("#f-h").hide() };
    – user229044
    Commented Sep 14, 2011 at 3:04
  • 1
    You could also use $('#field').is(':empty') as your expression for the if-statements.
    – Cᴏʀʏ
    Commented Sep 14, 2011 at 3:12
  • 3
    I'm going to have to use an empty if block sometime just for fun. :-P
    – Peter C
    Commented Sep 14, 2011 at 3:27
  • sorry your code was too ugly, I had to clean it up
    – vol7ron
    Commented Sep 14, 2011 at 3:38

3 Answers 3

2

My guess is that Internet Explorer is parsing the lines differently, resulting in an error. The solution to this is to always use semicolons to mark the ends of your lines. I also cleaned up the code a bit, replacing the empty if/else blocks with if-nots.

//hide webapp item if empty

if (!$("#features").html().trim()) {
  $("#f-h").hide();
}

if (!$("#application").html().trim()) {
  $("#a-h").hide();
}

if (!$("#tech-specs").html().trim()) {
  $("#t-h").hide();
}

if (!$("#downloads").html().trim()) {
  $("#d-h").hide();
}

if (!$("#custom-field").html().trim()) {
  $("#c-f").hide();
}
1
  • Thanks @Ben Lee and others, your code does look cleaner and more efficient, but the page still doesn't work in ie with the same errors. I edited the post to show the url if it does help.
    – user943741
    Commented Sep 14, 2011 at 3:19
1

jQuery html() method just returns a string containing the HTML.

JavaScript strings don't have a trim() method.

If you want to introduce one to the javascript's String object. You can do so with:

String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

^^^ didn't test this, but I am pretty sure it will do the job.

1
  • It did do the job! Cheers mate.
    – user943741
    Commented Sep 14, 2011 at 3:33
1

You could probably greatly simplify your JavaScript depending on the structure of your HTML. If the elements you wanted to hide were positioned consistently near/next to each other, you could assign classes to the sections ("features", "application", "tech-specs", etc.), e.g. "section", and the elements you wish to hide ("f-h", "a-h", "t-h", etc.), e.g. "hide-me", and use a single function to do the work:

$('.section').each(function() {
    if ($(this).is(':empty')) {
        $(this).closest('.hide-me').hide();
    }
});

I don't necessarily love this solution, but you could also use a map of the sections you want to test and hide if they are in random places in your HTML:

var items = [
    { key: '#features', value: '#f-h' },
    { key: '#application', value: '#a-h' },
    { key: '#tech-specs', value: '#t-h' },
    { key: '#downloads', value: '#d-h' },
    { key: '#custom-field', value: '#c-h' }
];

$.each(items, function(index, element) {
    if ($(element.key).is(':empty')) {
        $(element.value).hide();
    }
});

Here's a working example of the above code.

I know this doesn't answer your question but I just wanted to give my $0.02.

1
  • That's definitely more like $2.02 worth. I can see how much more efficient I could make the code. I am going to try and rewrite the code to work with your first suggestion, cheers!!
    – user943741
    Commented Sep 14, 2011 at 3:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.