Reputation: 1387
I want to append some elements with a button. But they have to have diffrent id's. Do i have to use an array? Like checkboxname[+i]. I'm using javascript
Thanks in advance
function append()
{
var cb = document.createElement( "input" );
cb.type = "checkbox";
cb.id = "id"
cb.checked = false;
var textfield = document.createElement( "input" );
var delbtn = document.createElement( "input" );
delbtn.type = "button";
delbtn.value = "remove";
delbtn.onclick= function(){remove()}
document.getElementById( 'append' ).appendChild( cb );
document.getElementById( 'append' ).appendChild( textfield );
document.getElementById( 'append' ).appendChild( delbtn );
Upvotes: 2
Views: 259
Reputation: 169383
You can use a counter to keep track of the id's
Given your code I've refactored and neated up a little using native JS & jQuery.
example: (using native JS):
example: (using jQuery):
Your not very specific. You could use a for loop but if you want it to happen on clicks of buttons a for loop isnt applicable
Upvotes: 3