why is if (element.innerHTML == "")
not working in firefox
but works fine in IE , any ideas please ?
why is if (element.innerHTML == "")
not working in firefox
but works fine in IE , any ideas please ?
Hard to say without seeing your HTML, but I'd say probably because you have some empty white space in the element, and IE doesn't treat that as a text node, while FF does.
I believe it is actually a more strict standards compliance to treat any empty white space between tags as a text node, but IE doesn't comply.
You could do:
var htmlstring = element.innerHTML;
// use the native .trim() if it exists
// otherwise use a regular expression
htmlstring = (htmlstring.trim) ? htmlstring.trim() : htmlstring.replace(/^\s+/,'');
if(htmlstring == '') {...
Or just get rid of the whitespace in your HTML markup manually.
You could check if element.innerHTML.trim() == ""
for the best results. However, then you have to extend the string prototype with a trim()
method:
if (!String.prototype.trim) {
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };
}
if (element.innerHTML.trim() == "") {
//do something
}
An alternative method to check for the empty string is to check the length:
element.innerHTML.length == 0
But, you'd still have to trim()
if you had a whitespace string you wanted to match.