jQuery Selector :first

Introduction

The :first selector selects the first element.

This selector can only select one single element.

The :first-child selector can select more than one element with one for each parent.

$(":first")

Select the first <p> element:

View in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("p:first").css("background-color", "yellow");
});/*from w w w.  j a  v a  2 s  . c o  m*/
</script>
</head>
<body>

<p>This is the first paragraph.</p>
<p>This is the second paragraph.</p>
<p>This is the last paragraph.</p>

</body>
</html>



PreviousNext

Related