PDA

View Full Version : setAttribute failing


weirdwes
12-08-2005, 04:31 PM
I'm using some AJAX techniques to read some data and append it to an existing table structure. The problem is the newly added rows are not formatted. I'm trying to append the class atrribute to the <TD> I'm creating and it fails every time. Help?


var tr = document.createElement('tr'); //create row
var td = document.createElement('td'); /create cell
var txt = document.createTextNode(abbr); //set text node to variable
td.setAttribute('Class','row1'); // this line errors, if I remove it the script works fine
td.appendChild(txt); // append text to cell
tr.appendChild(td); // append cell to row

vwphillips
12-08-2005, 05:44 PM
try

var tr = document.createElement('tr'); //create row
var td = document.createElement('td'); /create cell
var txt = document.createTextNode(abbr); //set text node to variable
td.appendChild(txt); // append text to cell
tr.appendChild(td); // append cell to row
td.className='row1';

Pyth007
12-08-2005, 06:47 PM
FYI: setAttribute() will also have problems with name and id attributes in IE; you'll also have to use dot-notation to set those values as well....

weirdwes
12-09-2005, 02:51 PM
Thanks for the suggestions guys. I ended up using the className thing to get it to work. I just think I had some screwy code in there somehwere.

Kor
12-09-2005, 04:30 PM
setAttribute() will also have problems with name and id attributes in IE

Never encountered this sort of problem on IE.

It's just IE uses setAttribute('className','myClass')
while Moz uses setAttribute('class','myClass')

so that the old DOM 0 level
object.className='myClass'
remains the simpliest cross browser solution

Pyth007
12-12-2005, 01:32 PM
Here (http://www.codingforums.com/showthread.php?t=72564)'s a thread that I had that actually examines class vs. className attributes...

As for the name / id problems in IE, I had read an examination of these attributes on some other site (although I can't find it now...). The reasoning seems to stem from:

IE doesn't completely distinguish between id's and names; you can call getElementById(objectName) feeding in the name of an object that does not have an id, and it will still retrieve that object...
IE defines many properties with empty values; other browsers keep properties undefines until they are set with a value...


I can't remember which version of IE this site claimed this problem was found on, so it might just be a problem with older versions and may have been fixed in IE6, but to be safe, I've just used DOM 0 dot-notation to set id and name attributes...