Selector –> :first used to select the first element.


Now suppose you want to select first p element on the page, then jQuery provides a very useful and powerful selector called ":first" that selects the first element. Its usage can be described as follows:

$("p:first")

The above syntax selects the first p element of our page. Now let us look at the short example:

<!--program starts-->

<html>

<head>
<script type='text/javascript'>
$("p:first").css("font-size","20px"); //makes the first p tag contents' size 20px
$("p:first").css("color","#f11"); //makes the text color "red"
</script>
</head>

<body>
<p>hello</p>
<p>See only first p tag is affected</p>
<p>These p tags are not affected</p>
</body>
</html>

<!--program ends-->

So the above program changes the color of content or text of first p tag into "red" with size "20px".
It only affects the first p tag of our page and have no effect on other p tags of our page.

0 0 votes
Article Rating
Subscribe
Notify of
guest

1 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
manish
manish
7 years ago

nice jquery article