Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

but works fine in IE , any ideas please ?

like image 453
Mahmoud Farahat Avatar asked Sep 09 '10 13:09

Mahmoud Farahat


People also ask

Why innerHTML does not work?

People can struggle and complain about innerHTML not working. Such things usually occur because of human error, when strings are not appropriately defined, or there are some mistakes in JavaScript code.

Does Firefox support innerText?

javascript - 'innerText' works in IE, but not in Firefox - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.


1 Answers

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.

like image 191
user113716 Avatar answered Oct 13 '22 20:10

user113716