views:

39

answers:

3

My question is, how can i use .removechild() in javascript without having the elements ID?

The reason I don''t have its ID, is because the td was only added using append child, which as far as i know, does not allow adding an ID as well, but i may be wrong?

I used :

var myRow = document.createElement("tr");

to make the child in my function at the start.

+2  A: 

You can add an id property to elements you create with document.createElement():

var myCol = document.createElement('td');
myCol.id = 'my-col-id';
myRow.appendChild(myCol);

Then you can use document.getElementById('my-col-id') without problems.

Daniel Vassallo
You also retain the pointer to the element from the declaration. Even after it is inserted the variable myRow (from your example) should still be a valid pointer to that object.
John
A: 

Yes, you can assign ID from code. Try like this:

myRow.id = 'the_id';
Fyodor Soikin
ahh ok, sorry for asking silly question, couldn't find that anywhere
mick waffle
@mick: Don't worry. The question is not silly. You may want to mark an answer as accepted by clicking on the green tick, if it was helpful to solve your problem.
Daniel Vassallo
A: 
myRow.id = 'someId'
Michael Robinson