Selecting elements by their tag name

In this post of JQuery, I will explain how to select elements by their tag name.

The syntax is as shown below


This will return one object or collection of more than one objects based on how many elements are there in the html matching the tagname.

In the below example when the button is pressed, “selectByTagName” javascript method is called and it will in turn call JQuery method as shown below


The above JQuery method will select all the elements with tag name ‘a’, which in the below example is 3.

Below is the complete code

            function selectByTagName() {
                var $element = $('a');
        <input id="button" name="button" type="button" value="button" onclick="selectByTagName();"/>
        <a class="class1"></a>
        <a class="class2"></a>
        <a class="class1 class3"></a>

Leave a Reply