Reputation: 165
How to create element with id name using javascript ?
I want to create <span id="inner">xxx</span>
inner <div id="outter">
I tried , but i cannot add id="inner"
to <span>
<!DOCTYPE html>
<html>
<body>
<div id="outer">
</div>
<script>
var para = document.createElement("span");
var node = document.createTextNode("xxx");
para.appendChild(node);
var element = document.getElementById("outer");
element.appendChild(para);
</script>
</body>
</html>
Upvotes: 1
Views: 7579
Reputation: 13
You can add id using two ways :
1) para.id="Id"; 2) para.setAttribute("id","Id");
Upvotes: 0
Reputation: 4218
You can also do it this way:
para.setAttribute("id", "inner");
Upvotes: 0