最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Create Checkbox with Javascript - Stack Overflow

programmeradmin0浏览0评论

I am trying to create a checkbox with javascript when a button is clicked. I am really struggling, I have searched the net for help and this is the closest I've got but it does not work.

What have I done wrong?

<p>Click the buttons to create a Checkbox.</p>

<button onclick="addCheckBox()">Create a button</button>
<input id="check" name="checkBoxes">
<script>
function addCheckBox() {

    var ColorsAvailable = document.getElementById('checkBoxes');
    var check_value = new Array( )
    check_value[0] = "Yellow"
    check_value[1] = "Red"
    check_value[2] = "Green"


   for(var count in check_value)
    {
      var color=document.createElement("input");   
      color.value=(check_value[count] + '</br>');
      color.type="checkbox";
      color.id="color" + count;
      ColorsAvailable.appendChild(color);
   }
}

</script>

I am trying to create a checkbox with javascript when a button is clicked. I am really struggling, I have searched the net for help and this is the closest I've got but it does not work.

What have I done wrong?

<p>Click the buttons to create a Checkbox.</p>

<button onclick="addCheckBox()">Create a button</button>
<input id="check" name="checkBoxes">
<script>
function addCheckBox() {

    var ColorsAvailable = document.getElementById('checkBoxes');
    var check_value = new Array( )
    check_value[0] = "Yellow"
    check_value[1] = "Red"
    check_value[2] = "Green"


   for(var count in check_value)
    {
      var color=document.createElement("input");   
      color.value=(check_value[count] + '</br>');
      color.type="checkbox";
      color.id="color" + count;
      ColorsAvailable.appendChild(color);
   }
}

</script>
Share Improve this question asked Jan 2, 2018 at 0:58 SudsSuds 371 silver badge7 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

After you add an element that has an id of checkBoxes and some semicolons ; the code actually works.

function addCheckBox() {

    var ColorsAvailable = document.getElementById('checkBoxes');
    var check_value = new Array();
    check_value[0] = "Yellow";
    check_value[1] = "Red";
    check_value[2] = "Green";

    var color, p, br;

   for(var count in check_value)
    {
      color=document.createElement("input");   
      color.value=(check_value[count] + '</br>');
      color.type="checkbox";
      color.id="color" + count;
      p =document.createElement("span");
      p.innerHTML = check_value[count] + ": ";
      br =document.createElement("br");
      ColorsAvailable.appendChild(p);
      ColorsAvailable.appendChild(color);
      ColorsAvailable.appendChild(br);
   }
}
input[type='checkbox']
{
   margin-right:20px;
}
<p>Click the buttons to create a Checkbox.</p>

<button onclick="addCheckBox()">Create a button</button>
<input id="check" name="checkBoxes">

<div id="checkBoxes"></div>

The id of the input was check and not checkBoxes. I changed it and also you can't directly append in the input so I made it append in the parent node but you could change to be on another place.

<p>Click the buttons to create a Checkbox.</p>

<button onclick="addCheckBox()">Create a button</button>
<input id="checkBoxes" name="checkBoxes">
<script>
function addCheckBox() {

    var ColorsAvailable = document.getElementById('checkBoxes');
    var check_value = new Array( )
    check_value[0] = "Yellow"
    check_value[1] = "Red"
    check_value[2] = "Green"


   for(var count in check_value)
    {
      var color=document.createElement("input");   
      color.value=(check_value[count] + '</br>');
      color.type="checkbox";
      color.id="color" + count;
      ColorsAvailable.parentNode.appendChild(color);
   }
}

</script>

Make checkboxes a DIV.

<div id="check" name="checkBoxes"></div>

and change this

var ColorsAvailable = document.getElementById('check');
发布评论

评论列表(0)

  1. 暂无评论