I have a button next to a text box and when I click the button i want it to create another button next to another text box. This code is doing just that but the new button doesn’t work.
<div id="algorithm">
<input type="button" id="add_child" value="+" />
<input type="text" id="textbox0" />
</div>
$('#add_child').click(function() {
$('<input type="button" id="add_child" value="+" />').appendTo('#algorithm');
$('<input type="text" id="textbox0" />').appendTo('#algorithm');
});
I’d suggest trying this approach:
JS Fiddle demo.
Revised the above code to update the
idattribute so that there are no duplicates:JS Fiddle demo.
References:
attr().clone().insertAfter().on().[attribute="value"]selector.