Thursday, June 20, 2024

Dynamically Create an H1 tag - Javascript Source code

Question

How can you use Javascript to accomplish the following task:
When a user clicks "Hello" button, how do you dynamically create an <h1> element and add it to a specific '<div>' element with the 'id' 'container' in the HTML document?


    <button onclick="adhelo()">add hello</button>
    <div id="divt"></div>
<script>
    var divt= document.getElementById("divt");

    function adhelo()
    {
        var newh1=document.createElement("h1");
        newh1.textContent="hello"
        divt.append(newh1);
    }
</script>

No comments:

Post a Comment

Change H1 text using buttons dynamically Javascript Sourcecode

Javascript Task How can i make it so that when a user clicks a button, the text from that button is shown...