0

I want to create a dynamic checkbox based on selected value text from drop-down list.

For example the drop-down list will have:

(test1, test2, test3)

So if I select test2 it will create a check box called test2 and so dynamically check box will get created.

2
  • 1
    Why do you need to create a checkbox when you already have the value from the select menu? I'm confused here. Commented Aug 28, 2012 at 5:29
  • Hi, this created check box will have side by 1 more linked button with few other details. and In the selected drop down list will more 5k records so whichever record wanted only will create a checkbox and go further functionalities Commented Aug 28, 2012 at 5:38

1 Answer 1

2

HTML

<div id="selectContainer">
    Step 1: 
    <select id="testSelect" name="test">
        <option></option>
        <option value="test1">Test 1</option>
        <option value="test2">Test 2</option>
        <option value="test3">Test 3</option>
    </select>
</div>
<div id="checkboxContainer"></div>​

JS

function callback(event) {
    var input     = document.createElement('input'),
        container = document.getElementById('checkboxContainer');

    if (!this.value &&  !event.srcElement.value) {
        return false;
    }

    input.type = 'checkbox';
    input.name = this.value || event.srcElement.value;

    container.innerHTML = '';
    container.appendChild(input);
}

try {
    // W3C
    document.getElementById('testSelect').addEventListener('change', callback);
} catch (e) {
    // Microsoft
    document.getElementById('testSelect').attachEvent('onchange', callback);
}

Demo: http://jsfiddle.net/VGAMR/

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.