assigning the id in html through the javascript DOM concept -


<body> <input type="button" value="add" onclick="add()"> <table id="mytable">  </table> <table id="mytable2">  </table> <table id="mytable3">  </table> </body> 

in above code creating 1 table dynamically programming part, based on result get.

my question, how can give id part in table dynamically?

ie table id="mytable3" ---> table3 given 3rd row element

2) table2 given 2nd row element

since cant have same id in same html creating sequentially , assigning id

can 1 suggest sample javascript code assigning id dynamically. can have better control later

in order need 1 simple dom traversal function. getelementsbytagname.

you can create purpose function loops through elements , adds id.

function addids(container, elname, prefix){     // container     var container=document.getelementbyid(container);     // tables     var tables=container.getelementsbytagname(elname);     // loop through tables     for(var i=0;i<tables.length;i++)         // add ids         tables[i].id=prefix+(i+1); } 

since doubt want affect elements in page, have created function takes container , looks inside elements specified tag , adds ids them prefix.

here's example of in action on jsfiddle.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -