Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Allocate an id to a cell which was created using JS

Status
Not open for further replies.

jdbolt

Programmer
Aug 10, 2005
89
CA
Hi,

I have some JS that creates a table

Code:
var docBody = document.getElementsByTagName("body").item(0);

docBody.appendChild(advancedSearchTable);

var advancedSearchTable = document.createElement("table");

// Create a table row
var row = advancedSearchTable.insertRow(advancedSearchTable.rows.length);
		
// This cell will hold the label
var label = row.insertCell(0);
		
// Style the label
label.id = 'fooCell'

label.innerHTML = "foo";

The trouhle is, is that it doesn't seem to allocate an id for the object with my script above. Where I go

Code:
alert(document.getElementById(label.id);

I get a message box with 'null'

however, using

Code:
alert(document.getElementById('content'));

does not return null, as content is the id of a div I have in teh HTML itself.

The same problem occurs in IE and FF, does anyone know a solution to this problem?

Thanks!
 
use addAttribute instead.

try this:

Code:
document.getElementById('label').setAttribute('id', 'fooCell');

:)

Regards.
 
Yeah thats works great, its wierd why it wasnt working before, maybe my crapping codin :)

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top