How to make checkbox events from dynamically created checkbox javascript

how to get value of dynamically created checkbox in javascript
dynamically create checkbox in jquery
dynamically create checkbox in javascript
javascript create checkbox with label
check checkbox dynamically javascript
javascript checkbox
dynamically created checkbox javascript checked
javascript render checkbox

I have three parameters namely model, destination and criteria. Whenever the user chooses a model from the dropdown list, where the destination and criteria is dependent, dynamic checkboxes for the destination is shown. And when a user tick a destination, its specific criteria will show. This is a follow up question: How to display multiple list of checkboxes dynamically on dropdown list

<script type="text/javascript">
    function populate(model, destination) {
        var mod = document.getElementById(model);
        var des = document.getElementById(destination);
        des.innerHTML = "";
        if (mod.value == "model-a") {
            var optionArray = ["Model-A.1", "Model-A.2", "Model-A.3"];
        } else if (mod.value == "model-b") {
            var optionArray = ["Model-B.1", "Model-B.2", "Model-B.3"];
        } else if (mod.value == "model-c") {
            var optionArray = ["Model-C.1", "Model-C.2", "Model-C.3"];
    }

    for (var option in optionArray) {
        if (optionArray.hasOwnProperty(option)) {
            var pair = optionArray[option];
            var checkbox = document.createElement("input");
            checkbox.type = "checkbox";
            checkbox.name = pair;
            checkbox.value = pair;
            des.appendChild(checkbox);
    
            var label = document.createElement('label')
            label.htmlFor = pair;
            label.appendChild(document.createTextNode(pair));

            des.appendChild(label);
            des.appendChild(document.createElement("br"));    
        }
    }
}
</script>
<!DOCTYPE html>
<html>
<body>

SELECT MODEL:
<select id="model" name="model" onchange="populate(this.id, 'destination')">
   <option value=""></option>
   <option value="model-a">MODEL-A</option>
   <option value="model-b">MODEL-B</option>
   <option value="model-c">MODEL-C</option>
</select>
<hr />
SELECT DESTINATION:
<div id="destination"></div>
<hr />
</body>
</html>

You may try my solution:

    <!DOCTYPE html>
<html>
<head>
<script>
 function populate(model, destination) {
        var mod = document.getElementById(model);
        var des = document.getElementById(destination);
        des.innerHTML = "";
        if (mod.value == "model-a") {
            var optionArray = ["Model-A.1", "Model-A.2", "Model-A.3"];
        } else if (mod.value == "model-b") {
            var optionArray = ["Model-B.1", "Model-B.2", "Model-B.3"];
        } else if (mod.value == "model-c") {
            var optionArray = ["Model-C.1", "Model-C.2", "Model-C.3"];
    }

    for (var option in optionArray) {
        if (optionArray.hasOwnProperty(option)) {
            var pair = optionArray[option];
            var checkbox = document.createElement("input");
            checkbox.type = "checkbox";
            checkbox.name = pair;
            checkbox.value = pair;

            //I added the below statement
            checkbox.onclick=updateCriteria;
            //I added the above statment                    

            des.appendChild(checkbox);

            var label = document.createElement('label')
            label.htmlFor = pair;
            label.appendChild(document.createTextNode(pair));

            des.appendChild(label);
            des.appendChild(document.createElement("br"));    
        }
    }
}
function updateCriteria()
{
    switch(this.value)
    {
        case "Model-A.1":alert(this.value+" is a dog.");
                         break;
        case "Model-A.2":alert(this.value+" is a cat.");
                         break;  
    }
    this.checked=false;
}
</script>
</head>
<body>

SELECT MODEL:
<select id="model" name="model" onchange="populate(this.id, 'destination');">
   <option value=""></option>
   <option value="model-a">MODEL-A</option>
   <option value="model-b">MODEL-B</option>
   <option value="model-c">MODEL-C</option>
</select>
<hr />
SELECT DESTINATION:
<div id="destination"></div>
<hr />
</body>
</html>

Dynamically Created Checkbox, Check Event Listener, One possible solution is to have to each checkbox an id, and in Javascript you can do this (if you know what the IDs will be):. var checkbox  To create a checkbox dynamically, you would need to create the checkbox, its label and optionally a <br> tag. 1. JavaScript. In pure JavaScript, you can use the document.createElement() method to programmatically create a checkbox element. The following example creates a new checkbox with necessary attributes and append it to a container using Node.appendChild() method.

you can use below code to attach event to the dynamically created element

var checkbox = document.createElement("input");
                checkbox.type = "checkbox";
                checkbox.name = pair;
                checkbox.value = pair;
                checkbox.id = "desCheckBox";
                des.appendChild(checkbox);

               //eventname is name of the event
               // here id is "desCheckBox"
                document.addEventListener('eventname', function (e) {
                    if (e.target && e.target.id == 'id') {
                        // do something
                    }
                });

Dynamically Create Checkboxes based on Textbox Values using , You can create checkboxes dynamically in JavaScript using the document. createElement() method. The method takes a parameter in the form of a tagName. Now let’s make a similar form with dynamically created checkboxes using values from a textbox. The textbox will provide values for the checkboxes and its labels. Therefore, I also need to create labels dynamically in my script. The Markup and Script. I have two input boxes of type text and button.

You could assign a class className to the checkbox you're creating and write an event applying to that class name. checkbox.className = "class_Name"

You can write this code at the level of your populate method.

$(".class_Name").unbind();
$(".class_Name").on("change", function () {
   //this will give you access to the checkbox item being changed
});

How to dynamically create a checkbox with JavaScript/jQuery , To create a checkbox dynamically, you would need to create the checkbox, its label and optionally a <br> tag. 1. JavaScript. In pure JavaScript, you can use the​  With the help of JavaScript, I have created a dynamic checkbox function. When i will click on create button it will dynamically create a checkbox item list and by clicking on checkbox item it will be removed easily.

[jQuery] How do I dynamically check a dynamically created checkbox?, I have a list of events in an SQL database, which are turned into spans placed inside labels labeled for checkboxes, all done in JS. How do I check the  Sets or returns whether a checkbox should automatically get focus when the page loads: checked: Sets or returns the checked state of a checkbox: defaultChecked: Returns the default value of the checked attribute: defaultValue: Sets or returns the default value of a checkbox: disabled: Sets or returns whether a checkbox is disabled, or not: form

JavaScript, I'm thinking I'm missing something that will make these checkboxes behave like they should or my approach to dynamic creation is flawed. In any event, my  To start working with checkboxes we will create a form that contains a checkbox , in this example we will be building a form that takes a user name and has also a checkbox that the user should check if he thinks he is fluent in English , if not a prompt will appear showing the user a text that says you need to be fluent in English to apply for the job.

How do I add event handler to dynamically created checkbox in , $('table').on('click', 'tbody > tr', function () {}): because table allready exists in the DOM when js  Create an event handler for the checkedChanged event of a check box iterate through the checkbox list. for each checbox in the check box list dynamiclly attach event handler to the CheckedChanged event of the check box. in vb.net it an be done through the AddHandler keyword.

Comments
  • the process is somehow like that but I need to display specific data like when checkbox1 is clicked, "dog" will be displayed. When checkbox2 is clicked, "cat" will be displayed. @the KNVB
  • it only mirrors the selected checkbox name. What I mean is that if I check, for example the Model-A.1, it will show that Model-A.1 is a dog. If I check Model-A.2, it will show that Model-A.2 is a cat. @the KNVB
  • you can use if - then - else or switch - case structure to do so.
  • is it possible? since the criteria will be dependent on the model and destination @the KNVB
  • Why not? Any difficulties you facing?