first() Method - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:first

Description

The first() method returns the first element of the selected elements.

Syntax

The following code shows how to Select the first <p> element inside the first <div> element:

Demo Code

ResultView the demo in separate window

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

<h1>Welcome to My Homepage</h1>

<p>My <span>name</span> is <span>span</span>.</p>
<p>I live in <span>span</span>.</p>
<p>My <span>span</span> friend is <span>span</span>.</p>

</body>
</html>

Related Tutorials