Skip to content

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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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

%d bloggers like this: