Tag Name
In this chapter you will learn:
- Syntax and Description for tag name selector
- Examples for tag name selector
- How to select all div tags
Syntax and Description
To select elements by tag name, use the tag name in the selector.
For example, $("div")
retrieves all of the divs in
a document.
The syntax of tag name selector is:
$('T')
$('T')
selects all elements that have a tag name of T
.
Examples
$('div')
selects all elements with a tag name of div in the document.$('em')
selects all elements with a tag name of em in the document.
The following code shows how to find every DIV element.
<html><!--from j av a 2 s . c o m-->
<head>
<script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("div").css("border","3px solid red");
});
</script>
</head>
<body>
<body>
<div class="myClass">div class="myClass"</div>
<span class="myClass">span class="myClass"</span>
</body>
</html>
Select all div tags
The following code use tag name to select all div tags.
<!DOCTYPE html> <!--from j a v a 2 s. c om-->
<html>
<head>
<script src="http://java2s.com/style/jquery-1.8.0.min.js">
</script>
<script>
$(document).ready(function(){
var wrappedElements = $("div");
document.writeln(wrappedElements.length);
});
</script>
</head>
<body>
<div id="1"></div>
<div id="2"></div>
<div id="3"></div>
</body>
</html>
Next chapter...
What you will learn in the next chapter: