Attribute equals ([foo=bar])
In this chapter you will learn:
- Description and Syntax for attribute equals selector
- Examples for attribute equals selector
- How to use two attribute selectors together
Description and Syntax
$('[name=theValue]')
selects all elements that have the 'name
'
attribute with a value exactly equal to theValue
.
Examples
Selector | Selects |
---|---|
$('[name=myname]') | all elements that have a name value exactly equal to myname |
$('a[rel=nofollow]') | all <a> elements that have a rel value exactly equal to nofollow |
The following code finds all inputs with name 'ab
'.
<html><!--from java 2 s. co m-->
<head>
<script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("input[name='ab']").val("ab");
});
</script>
</head>
<body>
<body>
<input name="ab" />
</body>
</html>
Use two attribute selectors together
The following code does the matching with two attribute names.
<html>//from java 2s . co m
<head>
<script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("input[id][name='data']").val("data");
});
</script>
</head>
<body>
<input id="end-data" name="data" />
</body>
</html>
Next chapter...
What you will learn in the next chapter:
- Description and Syntax for attribute not equal selector
- Examples for attribute not equal selector
- How to select input element by name attribute