Attribute selector:
We usually use either Class or ID to find html elements in jQuery but we can also use attribute to find specific elements. Attribute selector is written inside square brackets [attribute].
Some of the common attributes mostly used are −
- className
- tagName
- id
- href
- title
- rel
- src
Example:
<div class="main-blk">
<p>Sed ut perspiciatis <span title="legal">Legal</span> unde omnis</p>
<p>Sed ut perspiciatis unde omnis<span>justice</span> unde omnis</p>
<p>Sed ut perspiciatis unde omnis <span title="service">Services </span> unde omnis</p>
</div>
<script type="text/javascript">
$(function()
{
$("[title]").css({"color":"red","border-bottom":"2px solid grey","margin":"10px 0px","padding":"0px 5px"});
});
</script>
In the above example we are using title attribute to apply style using jQuery. Similarly we can use other attributes to find html elements using jQuery.
You can check above example output here: https://jsfiddle.net/bwzL83tc/4/
0 Comment(s)