Skip to content Skip to sidebar Skip to footer

Why Innerhtml Does Not Return True When Compared With Same String Value?

I have two tables on my html page with exact same data but there may be few difference which need to be highlighted. I and using the below Javascript but seems innerHTML does not w

Solution 1:

most browsers have bugs with innerHTML and it is not a recommended property to use. different browsers will do different things, usually messing with whitespace, adding/removing quotes, and/or changing the order of attributes.

long story short, never rely on innerHTML.

In it's place, I would recommend using some of the DOM traversal functions, such as .firstChild and .nodeValue. Notice that these are sensitive of white space, and will have to be tweaked if you have anything else in your TD than just text.

http://jsfiddle.net/tdN5L/

if (table.rows[i].cells[0].firstChild.nodeValue === columnValue)

Another option, as pointed out by Micah's solution, is using a library such as jQuery, which will let you ignore most of these browser issues and DOM manipulation pain. I would not recommend bringing in the overhead of jQuery just for this issue, though.

related:

Firefox innerHTML Bug?

innerHTML bug IE8

innerHTML removes attribute quotes in Internet Explorer

Solution 2:

Try and use Jquery's method .text

Depending on the browser(Firefox and Chrome's) innerHTML does not work

JQuery takes care of that issue for you.

Post a Comment for "Why Innerhtml Does Not Return True When Compared With Same String Value?"