jQuery | eq() Selector - trickcode

eq() Selector,Jquery Eq Selector,jquery Eq selector example,jquery Eq selector example explanation
Share it:
In this topic, we will learn a jquery Eq selector. jquery Eq selector allows us to select all elements at a specified index.

Jquery Eq Selector:-

jquery Eq selector matches a specific element by counting from the first element to the index value. Suppose that you want to choose the third <p> element on the page. Because the count starts with 0, to select the third <p> element, do the following.

$(‘p:eq(2)’).text(‘Changed this text.’);

jquery Eq selector example:-

In the following HTML example, we will see how to change the font style of <p> elements which is equal to the index value. To accomplish this task we can make use of : eq(index)



<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            $('p:eq(2)').css('font-family','Comic Sans MS');
        });
    </script>
</head>
<body>
<p>Paragraph 1-index[0]</p>
<p>Paragraph 2-index[1]</p>
<p>Paragraph 3-index[2]</p>
<p>Paragraph 4-index[3]</p>
<p>Paragraph 5-index[4]</p>
<p>Paragraph 6-index[5]</p>
</body>
</html>

output:

Paragraph 1-index[0]
Paragraph 2-index[1]
Paragraph 3-index[2]
Paragraph 4-index[3]
Paragraph 5-index[4]
Paragraph 6-index[5]

jquery Eq selector example explanation:-

$(‘p:eq(2)’).css(‘font-family’,’Comic Sans MS’); This line tells jquery to select <p> element which is equal to the index value. Here the index value is 2, so it will select the third <p> element. So jquery will change the third <p> elements font using CSS method.
Share it:

jQuery

Post A Comment:

0 comments: