How do I retrieve the value of a dynamically created control in CodeBehind using JavaScript?
I have generated the controls dynamically with the code snippet below:
let counter = 0;
let words;
let foo;//span tag
function add(i) {
let counter = 0;
let words;
let foo;//span tag asp in page where the controls to be added
if (i == 'ad') {
counter++;
//Creating an input type dynamically.
foo = document.getElementById("dynamic")
tbnam = document.createElement("input")
tbdes = document.createElement("input")
lbnam = document.createElement("Label")
lbdes = document.createElement("Label")
before = document.createElement('br')
after = document.createElement('br')
//Assigning different attributes to the elements.
wordsnam = document.createTextNode("Item")
wordsdes = document.createTextNode("Descrip")
tbnam.setAttribute("type", "TextBox");
tbdes.setAttribute("type", "TextBox");
tbnam.setAttribute("Id", "tbdynamicnam" + counter);
tbdes.setAttribute("Id", "tbdynamicdes" + counter);
lbnam.setAttribute("Id", "lbdynamicnam" + counter);
lbdes.setAttribute("Id", "lbdynamicdes" + counter);
before.setAttribute("Id", "bf" + counter);
after.setAttribute("Id", "af" + counter);
lbnam.appendChild(wordsnam)
lbdes.appendChild(wordsdes)
//Appending the element in page (in span).
foo.appendChild(before);
foo.appendChild(lbnam);
foo.appendChild(tbnam);
foo.appendChild(lbdes);
foo.appendChild(tbdes);
foo.appendChild(after);
}
}