Selecting HTML Tags (JS)

document.getElementById (id): Returns the first record matching the id attribute.
document.getElementsByName (name): Returns all records matching the name attribute.
document.getElementsByTagName: Returns all records that match the tag name as an array.
document.getElementsByClassName (className): Returns all records that match the class attribute.
document.querySelector: selects the first record that matches the selector.
document.querySelectorAll (selector): Return all records matching the selector to the array.


document.getElementById (id). Returns the first record matching the id attribute.
document.getElementsByName (name). Returns all records matching the name attribute.
document.getElementsByTagName. Returns all records that match the tag name as an array.
document.getElementsByClassName (className). Returns all records that match the class attribute.
document.querySelector. selects the first record that matches the selector.
document.querySelectorAll (selector). Return all records matching the selector to the array.


document.getElementById

<p id="tex">Codeblog</p>
    <script>
        var obj = document.getElementById("tex");
        document.write(obj)
    </script>

 document.getElementsByName

    <p name="tex">Codeblog</p>
    <script>
        var obj = document.getElementByName("tex");
        document.write(obj)
    </script>

document.getElementsByTagName

<p>Codeblog</p>
    <script>
        var obj = document.getElementsByTagName("p");
        document.write(obj)
    </script>

document.getElementsByClassName

<p class="tex">Codeblog</p>
    <script>
        var obj = document.getElementsByTagName("tex");
        document.write(obj)
    </script>

document.querySelector

<p class="tex">Codeblog</p>
    <script>
        var obj = document.querySelector(".tex");
        document.write(obj)
    </script>

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a website or blog at WordPress.com

Up ↑

Design a site like this with WordPress.com
Get started