Monday, August 18, 2008

Alternative solution for innerText for non IE browsers

In my application i was dynamically generating a html and my requirement was to get all the text inside a td and populate it in a select box using javascript, i was using innerText property in javascript and was populating all the contents, i sent it to QA department and the next day they sent it back saying its not working in firefox, then only i realised i should have checked in firefox, when i checked it in firefox it was showing only undefined...
then after googling i found a solution that works in IE, firefox and opera.
to get the innertext of a table element textcontent should be used, i have added a sample for reference

< html >
< head >
< script >
window.onload = function()
{
var text = (navigator.appName != 'Netscape') ? "innerText" : "textContent";
alert(document.getElementById('t1')[text]);
alert(document.getElementById('t2')[text]);
}
< / script >
< / head >
< table >
< tr >< td id='t1' >Row1 Col1< / td >< / tr >
< tr >< td id='t2' >Row2 Col1< / td >< / tr >
< / table >
< / html >

just save above code as a html file removing the spaces and open in all browsers vollah....

Cheers
Venkat

1 comment:

Rekha said...

gud info... thnx buddy :)