Skip to content Skip to sidebar Skip to footer

Copy Drop-down Lists When User Clicks Button In Javascript

How would I go about adding more drop down lists to a form, so when a user clicks a button a drop-down box will be added to the form? I'm guessing some sort of javascript? UPDATE:

Solution 1:

You can do this with .cloneNode().

Demo: http://jsfiddle.net/ThinkingStiff/pSEUH/

HTML:

<form id="form">
    <select id="select">
        <option>one</option><option>two</option><option>three</option>
    </select>
    <button id="add">Add</button>
</form>

Script:

document.getElementById( 'add' ).addEventListener( 'click', function ( event ) {

    event.preventDefault();
    var select = document.getElementById( 'select' ).cloneNode( true );
    document.getElementById( 'form' ).appendChild( select );

}, false );

Post a Comment for "Copy Drop-down Lists When User Clicks Button In Javascript"