Last updated Feb 17, 2021

8 Most useful Jquery Selectors - You Need To Know

Learn about the 8 most essential jQuery selectors that can help you efficiently navigate and manipulate HTML elements on your web pages.

8 Most useful Jquery Selectors - You Need To Know

Howdy coders,

In this tutorial, we are going to show some useful uses of Jquery selectors.

What is Jquery?

jQuery is a powerful and widely used JavaScript library that allows you to easily manipulate and navigate HTML elements on your web pages.

What are Selectors?

One of the key features of jQuery is its selectors, which enable you to identify and target specific HTML elements based on their attributes, tags, and positions in the DOM.

Selectors are a very useful part of jquery.

In Jquery selectors are used to select any HTML elements and perform some action on them Like add some animation on the element, make the element fade in-out or call some function on element click, etc….

Let’s start,

8 Most useful Jquery Selectors list

1. Attribute selector:

The attribute selector allows you to select elements based on their attribute values. For example, $('[data-name="John"]') will select all elements that have a data-name attribute with the value "John".

An attribute selector is used for the selected element which has the selected attribute.

For Example:

//select element which has title attribute
$('a[title]').fadeOut(2000)
//select element which has title=service attribute
$('a[title=service]').fadeIn(2000);

2. this selector:

This selector is used for selecting particular elements when an event occurred.

For Example:

//To select particular element when event occured 
$('p').click(function () {
   //This will hide slected elemets on click event
   $(this).hide();
});

Read, How to check-uncheck all checkboxes With jQuery

3. Odd-even selector:

Odd-Even Selector is used to select odd and even elements from HTML.

For Example:

//To select odd element
$('li:odd').addClass("green");
//To select even element
$('li:even').addClass("blue");

4.First last selector:

The first-last selector is used to select the first or last elements.

For Example:

//To select First Element 
$('li:first').addClass("green");
//To select Last Element
$('li:last').addClass("blue");

5.nth Element  selector:

nth Element selector is used to select any element according to its

For Example:

//To select nth element
$('li:eq(2)').addClass("green");

6. not Selector:

not selector is used when you want to not to select an element from given.

For Example:

//Do not select 3rd element
$('p:not(p:eq(2))').fadeOut(2000);

7. Form field selector:

The form field selector is used to select any form input

//To select all input fields
$(':input').addClass('green');
//To select input with type=password
$(':input:password').addClass('red');

8. Contains selector:

Contains selector is used to finding which elements contain selected text.

For Example:

$('#show').click(function(){
     //Find 'Jigar' from elements and show it		
      $("p:contains('Jigar')").show();
});

Also read, 12 Most Helpful jQuery Methods and Functions

Conclusion:

So, These are some basic uses of Jquery selectors. 

Thanks for reading.🙏

Do let me know If you face any difficulties please feel free to comment below we love to help you. if you have any feedback suggestions then please inform us by commenting.

Don’t forget to share this tutorial with your friends on Facebook and Twitter 

Tags

Available for projectsLet’s build together

Need this built properly,
not just explained?

I take on Angular and NestJS work directly — auth systems, multi-tenant SaaS, API rewrites and the deploy pipeline underneath them. You talk to the person writing the code.

Projects delivered
20+ Projects delivered
Angular • NestJS
7+ years Angular • NestJS
Average reply time
1 day Average reply time

What I can help with

  • Auth and permissions systems
  • Multi-tenant SaaS foundations
  • API design and rewrites
  • Deploy pipelines and VPS setup
  • Bug fixing and performance optimization
Send me a message

Tell me about your project and I’ll get back with a realistic scope and price.

Turn
Your Ideas
Into Reality

Stay in the loop

Get the finished tutorials, projects and resources in your inbox.

Join 5,000+ developers and get new tutorials, source code, product updates and exclusive content — straight to your inbox.

  • No spam
  • Useful content only
  • Unsubscribe anytime

Subscribe to TutsCoder

Be the first to get new tutorials, projects and tips.

Join 5,000+ developers already learning with us.

Learn
Build
Grow

Discussion

Comments

Share your thoughts, ask questions or suggest improvements. Let’s learn and grow together!

Loading comments…

Join the discussion

Sign in to join the discussion. Your account also keeps your saved tutorials and path progress.