I try the follow -
var input = document.createElement("<input type='checkbox' name='test'>")
but it prompt -
Uncaught InvalidCharacterError: The string contains invalid characters.
I use Chrome Version 33.0.1750.146 m
I try the follow -
var input = document.createElement("<input type='checkbox' name='test'>")
but it prompt -
Uncaught InvalidCharacterError: The string contains invalid characters.
I use Chrome Version 33.0.1750.146 m
- 1 See the doc w3/TR/2000/REC-DOM-Level-2-Core-20001113/… – gernberg Commented Mar 6, 2014 at 22:04
- MDN usually has nice explanation of DOM methods: developer.mozilla/en-US/docs/Web/API/document.createElement – Ian Commented Mar 6, 2014 at 22:04
2 Answers
Reset to default 7You create the plain element and then add the attributes:
var input = document.createElement("input");
input.type = "checkbox";
input.name = "test";
The document.createElement function takes a tag name for the element you want to create (in your case input)
var input = document.createElement("input");
you can then access attributes for that element like so:
input.type = "checkbox";
input.name = "test";
Here is another question with the same answer: How to create <input type=“text”/> dynamically