why if (element.innerHTML == "") is not working in firefox

Mahmoud Farahat picture Mahmoud Farahat · Sep 9, 2010 · Viewed 39.3k times · Source

why is if (element.innerHTML == "") not working in firefox

but works fine in IE , any ideas please ?

Answer

user113716 picture user113716 · Sep 9, 2010

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.