开发者

Array needed for appended checkbox list?

开发者 https://www.devze.com 2023-01-28 05:52 出处:网络
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 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( 开发者_StackOverflow社区textfield );
        document.getElementById( 'append' ).appendChild( delbtn );


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

0

精彩评论

暂无评论...
验证码 换一张
取 消