admin管理员组文章数量:1313800
Am trying to write a simplest code, like onclick of button a new textbox with unique id should be assigned and appended to the existing ones.
function appendRow()
{
var x = 1;
for(var i=0; i < x; i++)
{
var d = document.getElementById('div');
d.innerHTML = "<input type='text' id='tst"+ x +"'><br >";
}
++x;
}
HTML:
<div id="div">
<button onclick ="appendRow()" value="Add Row">Add Row</button>
</div>
This might be very simple for techies.
Thanks.
Am trying to write a simplest code, like onclick of button a new textbox with unique id should be assigned and appended to the existing ones.
function appendRow()
{
var x = 1;
for(var i=0; i < x; i++)
{
var d = document.getElementById('div');
d.innerHTML = "<input type='text' id='tst"+ x +"'><br >";
}
++x;
}
HTML:
<div id="div">
<button onclick ="appendRow()" value="Add Row">Add Row</button>
</div>
This might be very simple for techies.
Thanks.
Share Improve this question asked Sep 18, 2014 at 10:52 BaskyBasky 1011 gold badge3 silver badges8 bronze badges 1- Aside from your code replacing the button instead of appending, what doesn't work in the code? – Ynhockey Commented Sep 18, 2014 at 10:54
1 Answer
Reset to default 4You are replacing the button. You have to append the new div to the already created elements. Try to use +=
instead of =
.
Also there is not need for a loop. You can assign unique id with just an inceasing var
Try:
var x=1
function appendRow()
{
var d = document.getElementById('div');
d.innerHTML += "<input type='text' id='tst"+ x++ +"'><br >";
}
DEMO
本文标签: javascriptAppending a new textbox onclick of buttonStack Overflow
版权声明:本文标题:javascript - Appending a new textbox onclick of button - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741956085a2406995.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论