2

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 );
0

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.