I want to generate dynamically a few elements (not known in advance how many) and glue them together and essentially extend a div, say with id "myDiv". For demonstration purposes, say that a client receives a message to generate a list of three buttons on the fly by extending the div.
I am stuck though, because I generate the list like this
var temp = "<ul>";
for (i = 0; i < someTimes; i++) {
temp += "<li>some text <div id=\"button" + i + "\"></div></li>";
}
temp += "</ul>";
then do
document.getElementById('myDiv').innerHTML = html;
and in order to generate each button my initial intention was to then do this in order to append each button to the appropriate div that was generated on the fly in the while loop. But this thing does not work. Perhaps there are a few things here for a newbie like me;
- Is there a Javascript example that I could use inside the for loop in order generate the buttons dynamically, give them the id that I want and delegate them?
- Should I convert the first part from Javascript to jQuery and then do both things simultaneously with jQuery? How (in this example)?
- Is there a good reference for these things?
Thank you in advance for your time. I do not expect answers to all three questions but I would like to get an answer on how to achieve this mini goal as a guideline on how to continue my work.