Header Ads

  • Breaking News

    JavaScript Introduction

    https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS2y_zeHfDTcd1fAp5UEYk002D6uytkCNn9h4diiZnZhGyz61on

     

    JavaScript Can Change HTML Content

    One of many JavaScript HTML methods is getElementById().
    This example uses the method to "find" an HTML element (with id="demo") and changes the element content (innerHTML) to "Hello JavaScript":

    Example:



    <!DOCTYPE html>
    <html>
    <body>

    <h2>What Can JavaScript Do?</h2>

    <p id="demo">JavaScript can change HTML content.</p>

    <button type="button" onclick='document.getElementById("demo").innerHTML = "Hello JavaScript!"'>Click Me!</button>

    </body>
    </html>Example 2:
    <!DOCTYPE html>
    <html>
    <body>

    <h2>What Can JavaScript Do?</h2>

    <p>JavaScript can change HTML attributes.</p>

    <p>In this case JavaScript changes the src (source) attribute of an image.</p>

    <button onclick="document.getElementById('myImage').src='pic_bulbon.gif'">Turn on the light</button>

    <img id="myImage" src="pic_bulboff.gif" style="width:100px">

    <button onclick="document.getElementById('myImage').src='pic_bulboff.gif'">Turn off the light</button>

    </body>
    </html>

    1 comment:

    Post Bottom Ad