4

I've been working at this for the last hour, but I am still unable to get the expected output... All I want is to create an HTML element using the onclick event. I was able to create the element on load, but find myself unable to with the event. Please lead me in the right direction. Here's my HTML page:

<html>
<body>

    <div id="d1">
        <p id="p1">Paragraph.</p>

    </div>
    <div id="d2">
        <label onclick="open()">Inbox</label>
    </div>

    <script>
        function open(){
            alert("Start");
            var para=document.createElement("p");
            var node=document.createTextNode("Text");
            para.appendChild(node);
            para.style.color="red";
            var element=document.getElementById("d2");
            element.appendChild(para);
                       }
    </script>

    </body>
</html>

3 Answers 3

4

open() seems to be a reserved functions. Tried with open1(), it works.

Sign up to request clarification or add additional context in comments.

1 Comment

Excatly open is reserved thanx
2

You are probably colliding with the window.open function built into the browser, rename your function.

1 Comment

thnaks for the answer...yes it was reserved word..as Shashank said...and u cleared that it was window.open
0

Try this code

<label onclick="create_elem();">Inbox</label>
<script language="javascript">
function create_elem(){
    alert("Start");
    var para=document.createElement("p");
    var node=document.createTextNode("Text");
    para.appendChild(node);
    para.style.color="red";
    var element=document.getElementById("d2");
    element.appendChild(para);
}
</script>

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.