Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript element.style is undefined in FF

I want to set up css display property in javascript code:

    var div = document.createElement('div');
    div.innerHTML = content;
    div.childNodes[0].style.display = '';

It works in IE but doesn't in FF. It says "style" is undefined for element div. How can I do it in FF?

Thanks

like image 500
ihorko Avatar asked Jun 16 '26 16:06

ihorko


1 Answers

What is content? If it starts with white space, then there will be a TextNode as the first child and they don't have style properties (HTMLElementNodes do).

You can either:

  • loop over the children until you either get to the end or find an HTMLElementNode
  • strip the whitespace from the start of content
  • switch to using createElement and friends instead of innerHTML
like image 128
Quentin Avatar answered Jun 19 '26 04:06

Quentin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!