Getting INVALID_CHARACTER_ERR: DOM Exception 5

13,912

document.createElement takes the tag name only as its parameter, you'll have to set the type and value after

var task = document.createElement("input")
task.type = "checkbox";
task.value = textBox.value;

Also input tags are empty, there are no closing tag or inner html, the value is set as an attribute in markup.

Share:
13,912
Rafael Adel
Author by

Rafael Adel

About me

Updated on June 04, 2022

Comments

  • Rafael Adel
    Rafael Adel almost 2 years

    I'm writing a simple to-do list. that a user input a text and the it's added as a checkbox. But i'm getting this error i have no idea what's it about

    INVALID_CHARACTER_ERR: DOM Exception 5
    
    window.onload = function(){
        var textBox = document.getElementById("taskInput"),
            submitBtn = document.getElementById("submit"),
            taskPool = document.getElementById("todoTask");
    
        submitBtn.addEventListener("click", function(){
            var task = document.createElement("<input type=\"checkbox\">" + textBox.value + "</input>");
            taskPool.appendChild(task);
        });
    
    }
    
  • Rafael Adel
    Rafael Adel almost 12 years
    ah, that worked. But how can i add a label inside each box ? because a checkbox without text appears when i press the button.
  • Musa
    Musa almost 12 years
    @Rafael you don't put labels inside cheeckboxes you put it next to them and use the label's for attribute to specify which checkbox the label is for.
  • Musa
    Musa almost 12 years
    @Rafael The property for the for attribute is htmlFor see http://jsfiddle.net/mowglisanu/6gbSE/4/
  • Rafael Adel
    Rafael Adel almost 12 years
    Ya, i just figured it out. Thanks a lot :)