Jquery selectors

Advertisements

Prev Tutorial Next Tutorial

Selectors

jQuery selectors are used to find or select Html elements based on their id, classes, types, attributes, values of attributes etc.. This jQuery selector is based on css selector but it have some own custom selectors.

Selector in jQuery is start with $ sign and parentheses: $()

Syntax

$(selector).action()

Element Selector

jQuery element selector selects or find Html elements based on the element name. In below syntax you can select all <p> elements.

Syntax

$("p")

Example of element selector

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $("p").hide();
    });
});
</script>
</head>
<body>

<h2>jQuery Code</h2>

<p>This is My first jQuery code</p>

<button>Click me</button>

</body>
</html>

Output

jQuery Code

This is My first jQuery code

#id Selector

jQuery id selector finds elements with a specific id. In below syntax you can find or select all elements which have list id.

Syntax

$("#list")

Example of #id selector

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $("#list").hide();
    });
});
</script>
</head>
<body>

<h2>jQuery Code</h2>

<p id="#list">This is My first jQuery code</p>

<button>Click me</button>

</body>
</html>

Output

jQuery Code

This is My first jQuery code

.class Selector

jQuery class selector finds elements with a specific class. In below syntax you can find or select all elements which have list class.

Syntax

$(".list")

Example of .class selector

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $(".list").hide();
    });
});
</script>
</head>
<body>

<h2>jQuery Code</h2>

<p id=".list">This is My first jQuery code</p>

<button>Click me</button>

</body>
</html>

Output

jQuery Code

This is My first jQuery code

Examples of jQuery Selectors

  • $("*") : Selects all elements.
  • $(this) : Selects the current Html element.

Prev Tutorial Next Tutorial

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements