Jquery Selectors


jQuery selectors are used to find or select HTML elements based on their class, id, value, type or attribute.

jQuery offers a powerful set of tools for matching a set of elements in a document.

Jquery selectors are start with the: $().

[code language=”html” line=”1″ highlight=”3″]
<p>All Selector (“*”)</p>
<p><li>Selects all elements.</p>
[/code]

Demo – All Selector

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>all demo</title>
<style>
h3 {
margin: 0;
}
div, span, p {
width: 80px;
height: 40px;
float: left;
padding: 10px;
margin: 10px;
background-color: #EEEEEE;
}
#test {
width: auto;
height: auto;
background-color: transparent;
}
</style>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
</head>
<body>
<div id="test">
<div>DIV</div>
<span>SPAN</span>
<p>P <button>Button</button></p>
</div>
<script>
$( document ).ready(function() {
var elementCount = $( "#test" ).find( "*" ).css( "border", "3px solid red" ).length;
$( "body" ).prepend( "<h3>" + elementCount + " elements found</h3>" );
});
</script>
</body>
</html>

[code language=”html” line=”1″]
<strong>$("#demo").hide();</strong>
<ul>
<li>hide() method hidies all the elements with id="demo".</li>
</ul>
<strong>$(".demo").hide();</strong>
<ul>
<li>hide() method hides all he elements with class="demo".</li>
</ul>
<strong>$(this).hide();</strong>
<ul>
<li>hide() method hides the current HTML element.</li>
</ul>
[/code]

SHARE: