Saturday, June 22, 2024

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 in the hq tag located above it?

Tips: using event

 

    <div>
        <h1 id="h1n">Name</h1>
        <button onclick="update(event)">John</button>
        <button onclick="update(event)">King</button>
        <button onclick="update(event)">Queen</button>
    </div>
    <script>
        var h1name=document.getElementById('h1n');
        function update(event)
        {
             
           console.log(event.target.textContent);
            h1name.textContent=event.target.textContent;
        }
    </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...