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

javascript - How to create an <input> using document.createElement()? - Stack Overflow

programmeradmin3浏览0评论

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

Share Improve this question asked Mar 6, 2014 at 22:02 URL87URL87 11k36 gold badges111 silver badges177 bronze badges 2
  • 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
Add a ment  | 

2 Answers 2

Reset to default 7

You 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

发布评论

评论列表(0)

  1. 暂无评论