Opoe
Opoe

Reputation: 1387

Array needed for appended checkbox list?

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

Answers (1)

Raynos
Raynos

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):

http://jsfiddle.net/4Y8mb/34/

example: (using jQuery):

http://jsfiddle.net/4Y8mb/18/

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

Related Questions