In this post we are learn about select a
Set of Elements in jQuery.
When we pass a
selector to the
jquery( ) function— or the
$( ) function, which is the
same thing—we select a set of
page elements.
In this post, we will select all the <p> elements
in a page using the selector "p", like this:
$("p"). This selector returns a set of all <p>
elements. We will count the number of <p>
elements in the set with the jQuery size( )
function and display that number in an alert box.
jqscript2.2.html
<html>
<head>
<title>Count paragraphs</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
function count( )
{
alert("There are " + $("p").size( )
+ " paragraphs.");
}
</script>
</head>
<body>
<h1>Count paragraphs</h1>
<div>
<p>This is paragraph 1.</p>
<p>This is paragraph 2.</p>
<p>This is paragraph 3.</p>
<p>This is paragraph 4.</p>
</div>
<form>
<input type="button" value="Count
Paragraphs"
onclick="count( )"
</input>
</form>
</body>
</html>
Output:
No comments:
Post a Comment
Please do not enter any spam link in the comment box.