Hi Reader's,
Welcome to FindNerd, today we are going to discuss how to use jQuery :first Selector?
Basically :first selector is used for selecting the first element and it is always use in a group.
The :first selector can only select one single element.
syntax of :first Selector
$(":first")
You can see below example for better understanding:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<!-- start css part -->
<style>
#paragraph {
max-width: 600px;
margin: 0 auto;
color:black;
}
</style>
<!-- end css part -->
<script>
$(document).ready(function(){
//call here :first Selector and also put css for effect
$("p:first").css("background-color", "green");
});
</script>
</head>
<body>
<div id = "paragraph">
<p>First paragraph.</p>
<p>Second paragraph.</p>
<p>Last paragraph.</p>
</div>
</body>
</html>
In above code you can see First Parameter will be selected with green color.
0 Comment(s)