mercredi 1 février 2017

How to add a checked checkbox element into Div dynamically?

I want to make a JavaScript function, which, after pressing a button, takes the list of checkbox elements with their content, checks all the checkboxes, creates a div element with these checkboxes and writes the result to the HTML form.

Here is my code:

function  confirmDrivers() {     
        $('#selectedList').find('.chk').prop("checked", true);
        var list = document.getElementById('selectedList').getElementsByTagName("li");
        var myForm = document.getElementById('formInput');
        var text = "<strong>Selected Drivers: </strong> <br><br>";
        var myDiv = document.createElement("div");
        myDiv.setAttribute("id","selectedInputDrivers");
        myDiv.style.overflowY = "auto";
        myDiv.style.maxHeight = "100px";
        myDiv.style.maxWidth = "250px";


        for (i = list.length - 1; i >= 0; i--)
        {                        
              myDiv.innerHTML = list[i].innerHTML+'<br>'+myDiv.innerHTML;                 
        }            
        $("formInput").find('.chk').prop("checked", true);
        myForm.innerHTML = myDiv.outerHTML + myForm.innerHTML;
        myForm.innerHTML = text + myForm.innerHTML;
    }

The problem here is that it checks all the checkboxes in my list, but in the resulting HTML form they appear unchecked again. What is wrong with it? Thank you




Aucun commentaire:

Enregistrer un commentaire