In Interviews, when an interviewer asks basic questions, we frequently give the right answers but many times, we are not familiar with the nomenclature. The same thing happens with jQuery Selector. I use these selectors very often but do not know their nomenclature.
jQuery Selectors are used for selecting element(s) and modifying the HTML elements (Like div, h1, p etc.).
Before going forward, we all need to know what the dollar sign($) is in jQuery, because jQuery Selectors use the $ sign to find or select HTML element(s) based on their name, id, attributes, classes etc.
Dollar ($)
It references to defining or accessing the jQuery, which means when you call it, it will check if the jQuery is defined or not and is it valid for that page or not.
Type of jQuery Selectors
- Element Selector
It selects an element based on the element name. (Eg. $("div") )
- #ID Selector
It selects an element based on the ID of the element. We need to add "#" before the ID of the element that we want to select. It is the same as we used in CSS ( Eg. $("#controlID") )
- Class Selector
When we want to access or select an element which uses a class applied to any control. (Eg. $(".gridStyle") ). It starts with a full stop ".".
- All Selector
If we want to select all HTML elements, then we use "*". (Eg. $("*") ).
There are many more selectors used in jQuery, that can be used as per our requirement.