1

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>

3 Answers 3

4

Javascript second line add this code:

para.id = "inner";
Sign up to request clarification or add additional context in comments.

Comments

0

You can also do it this way:

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

Comments

0

You can add id using two ways :

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.