Menu

1 / 5

DOM Exercise

DEV Community·mohandass·7 months ago
#9uVNMTcH
#beginners#html#javascript#webdev#button
Reading 0:00
15s threshold

EXERCISE 1.Change Text on Button Click

PROGRAM:

<body>

<h1 id= "text" > welcome JavaScript </h1> <button id= "but" onclick= "changetext()" > change </button> <script>

const heading = document . getElementById ( " text " ) function changetext () { heading . innerText = " welcome DOM " ; }

</script>

Enter fullscreen mode

Exit fullscreen mode

OUTPUT:

EXERCISE 2. Light ON/OFF

PROGRAM:

<body> <img id= "bulb-img" src= "https://www.w3schools.com/js/pic_bulboff.gif" alt= "" > <button style= "color: green; padding: 10px 15px; border-radius: 20px; font-size: medium;" id= "bnt1" onclick= "bulb_change()" > ON </button> <!-- <button id="butt2" onclick="bulb_normal()">off</button> -->

<script>

function bulb_change () { const bnt = document . getElementById ( " bnt1 " ) const bulb = document . getElementById ( " bulb-img " )

if ( bulb . src == " https://www.w3schools.com/js/pic_bulboff.gif " ){ bulb . src = " https://www.w3schools.com/js/pic_bulbon.gif " bnt . innerText = " OFF " ; bnt . style .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.