How to change table cell id using javascript

I tried this but did not work.

tbl1.rows[0].cells[0].id ='myId'; 

and it also does not work

 tbl1.rows[0].cells[0].setAttribute("id","newid"); 

How can i do this?

EDIT: When viewing the source of the page, the identifier does not change, but when we check the use of Firebug, a new identifier appears.

thanks

+4
source share
3 answers

That should work. If this is not the case, there may be an error somewhere else, or the selector is not correct.

See a working demo .

Edit

Just found that this does not work in firefox.

Try

 document.getElementById("tbl1").rows[0].cells[0].id = "myId"; 
+2
source

Are you sure tbl1.rows[0].cells[0] defined? Because if this is a valid DOM element, just setting the id property should be enough.

0
source

If tbl1 points to an existing DOM table entry, this should work. Do you expect the page to load before the destination tbl1? Tbl1 may not be receiving the intended use.

0
source

Source: https://habr.com/ru/post/1308326/


All Articles