Consider that i have a variable var=output[] and this output will sometimes have one output or multiple outputs. Now, i want to create a checkbox based on the output. If the out is of one then one checkbox has to be created with output name and if if the output is multiple then multiple check boxes has to created with output names. Please help me out and can you make it just JavaScript and not with any another.
-
2Your question is pretty unclear, please share what you have tried so far.APAD1– APAD12018-11-19 17:04:19 +00:00Commented Nov 19, 2018 at 17:04
-
Unfortunatly i havent tried anything. I do not know where to start all i have is a output and some output will be of one that is output = cat and then i have create a checkbox with cat name next to it. sometimes ouput will be multiple like cat,dog,snake and then 3 checkboxes has to be created.john mathew– john mathew2018-11-19 17:24:10 +00:00Commented Nov 19, 2018 at 17:24
-
i believe this is what your looking for stackoverflow.com/questions/2055459/…rrrm93– rrrm932018-11-19 17:31:24 +00:00Commented Nov 19, 2018 at 17:31
-
yes kind of and the code is in jqueary i guess. Can i get the same code in pure javascript? I am pretty new javascript..john mathew– john mathew2018-11-19 18:04:22 +00:00Commented Nov 19, 2018 at 18:04
-
Instead of using jquery, just use getElementbyId().innerHTML and add the actual text of the checkbox, like "<input type='checkbox'/>".dev4life– dev4life2018-11-19 18:12:01 +00:00Commented Nov 19, 2018 at 18:12
Add a comment
|
1 Answer
Heres the code in javascript
<div id="cblist">
<input type="checkbox" value="first checkbox" id="cb1" /> <label for="cb1">first checkbox</label>
</div>
<input type="text" id="txtName" />
<input type="button" value="ok" onClick="addCheckbox()" id="btnSave" />
function addCheckbox(name) {
var container = document.getElementById('cblist');
var name = document.getElementById('txtName').value
var input = document.createElement("input");
input.setAttribute("type", "checkbox");
input.setAttribute("value", name);
input.setAttribute("name", 'cb' + name);
container.appendChild(input);
var label = document.createElement("label");
label.htmlFor = "cb" + name;
label.innerHTML = name;
container.appendChild(label);
}