JavaScript > Dealing with HTML Controls

Finding element using ID in JavaScript

How to find a HTML element using on it’s ID in JavaScript?


To find the html controls by their id attribute, we can follow this approach.

    <script>
        function TestFunction() {
            alert(document.getElementById("lblText").innerHTML);
        }
    </script>

    <label id="lblText">0</label>
    <input type="button" id="btnTest" onclick="TestFunction()" value="Click me" />

In the above function, getElementById finds a label with id “lblText” and shows its inner HTML content as an alert.

OUTPUT

 Views: 4125 | Post Order: 57



Write for us






Hosting Recommendations