So I just created a textbox with JavaScript like this:
EDIT: Added the len variable
var len = tbl.rows.length; var rtb = tbl.insertRow(len); var cName = rtb.insertCell(0); var cDis = rtb.insertCell(1); var cDur = rtb.insertCell(2); cName.innerHTML = '<input type='text' name='tbName1' + len + '' value='' + selected_text + '' >'; cDis.innerHTML = '<input type='text' name='tbDis1' + len + '' id='tbDis1' + len + '' >'; cDur.innerHTML = '<input type='text' name='tbDur1' + len + '' >'; var txtBox = document.getElementById('tbDist1' + len); txtBox.focus();
EDIT:Changed the second to last line. Still get this error: txtBox is null txtBox.focus();
The last line isn’t working. After I create the textbox, I can’t set focus to it. Is there any way of doing so?
Hum… you’re creating the textbox by saying
id='tbDis1' + len + ''but you’re accessing it by doing'tbDist1' + (len - 1)… why? I am not sure about the context, but that would try to focus the previously added textbox, if any. Also, you’re creating it withtbDisand trying to get to it by usingtbDist. Missing atin there. Setting the id asid='tbDist1' + len + ''and accessing it with'tbDist1' + (len)should do the trick.