robert qewerutiyo
robert qewerutiyo

Reputation: 165

How to create element with id name using javascript?

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>

http://jsfiddle.net/rndao3rd/

<!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

Answers (3)

shah bhaumik
shah bhaumik

Reputation: 13

You can add id using two ways :

1) para.id="Id"; 2) para.setAttribute("id","Id");

Upvotes: 0

Luthando Ntsekwa
Luthando Ntsekwa

Reputation: 4218

You can also do it this way:

para.setAttribute("id", "inner");

Upvotes: 0

egvrcn
egvrcn

Reputation: 984

Javascript second line add this code:

para.id = "inner";

Upvotes: 4

Related Questions