Simply put, jQuery selector allows you to find (match) a set of elements in a document. jQuery selectors provides you additional syntax for matching elements in addition to the ones provided by CSS (1 – 3). A complete list of selectors provided can be found at http://api.jquery.com/category/selectors/.
In an interview, or while solving a problem that involves jQuery, you have to understand and use selectors to match elements to do the necessary operations. Simple selectors can be grouped into a few easy to remember categories: Basic, Forms, Attribute, hierarchy, filters and so on. The selector grammar has three layers. “#test” selects an element with an id attribute of “test”, “p” selects all <p> tags in the document, and “div.status” selects all <div> tags with a class attribute of “status”. Simple selectors can also be combined together to form more complex selections.
Explain the basic jQuery selectors
The following selectors are based on the CSS 1 specification, as outlined by the W3C.
-
All Selector (“*”) Selects all elements.
-
Class Selector (“.class”) Selects all elements with the given class.
-
Element Selector (“element”) Selects all elements with the given tag name.
-
ID Selector (“#id”) Selects a single element with the given id attribute.
-
Multiple Selector (“selector1, selector2, selectorN”) Selects the combined results of all the specified selectors.
Find the element with the class "myClass".
$(".myClass").css("border","3px solid red");
Finds every DIV element.
$("div").css("border","3px solid red");
Finds the element with the id "myDiv".
$("#myDiv").css("border","3px solid red");
Finds the elements that match any of these three selectors.
$("div,span,p.myClass").css("border","3px solid red");
Explain the Form based selectors
The form selectors allow you to select elements of a <form>. the form based selectors return an array of jQuery objects containing the list of matching elements. The general syntax is $(“:<controlName>”) where control name is one of the following: button, checkbox, file, hidden, image, input, password, radio, reset, submit and text.
var input = $(":checkbox").css({background:"yellow", border:"3px red solid"});
While we are on the subject of forms, let’s review a few form filters. A good question I like to ask is: Find all the input elements that are checked on the page. The answer is simple. We use the form filter selection mechanism.
function countChecked()
{
var n = $("input:checked").length;
$("div").text(n + (n == 1 ? " is" : " are") + " checked!");
}
countChecked();
$(":checkbox").click(countChecked);
Explain Attribute filter selectors
$("input[name*='man']").val("has man in it!");
$("input[name$='letter']").val("a letter");
$("input[id][name$='man']").val("only this one");
jQuery selectors is perhaps one of the largest topics to cover. This post should give you enough background to grasp and understand the basics. You should look at the jQuery Selector API documentation to dig more.
Nice tips, also check out this little tutorial for extending and creating your own selectors: http://www.websanova.com/tutorials/jquery/12-awesome-jquery-selector-extensions
ReplyDeleteThanks a tone vey awesome airticle
ReplyDeleteHi There,
ReplyDeleteWhat a brilliant post I have come across and believe me I have been searching out for this similar kind of post for past a week and hardly came across this.
hi i have a table with two columns as primary key i what to trap exception if the use in UI try to enter duplication in the table i don't what to throw ORA ERROR i what to display meaningful error massage,my database is oracle 11g
this is how i what to do it
i what to add integrity constraint to the table, and i use explicitly name it.
- i test the database to figure out what exception it returns for that constraint. With luck the name i used will be in the text of the exception.
Anyways great write up, your efforts are much appreciated.
Thanks and Regards
Irene Hynes
-------------------------------------------------------------------------------------------------------
Hi There,
Amaze! I have been looking bing for hours because of this and i also in the end think it is in this article! Maybe I recommend you something helps me all the time?
Okay, so I have to make a programm where the user gives you the bank sorting code and the account number and you give him the IBAN. That was so far no problem and I was done within minutes except of one thing that I simply can't figure out even though im trying since weeks. At some point I have to convert a string to integer. My research told me its with parseInt() and I dont get a syntax error when I compile my programm (using BlueJ). But when executing the programm stops and gives me some weird bug message. Anywhay without further ado I really hope you guys can help me, because I am growing desperate Here is my code and my bug message:
Thanks a lot. This was a perfect step-by-step guide. Don’t think it could have been done better.
,Merci
Irene Hynes
Mua vé máy bay tại Aivivu, tham khảo
ReplyDeleteVé máy bay đi Mỹ
có vé máy bay từ mỹ về việt nam không
vé máy bay từ tpHCM đi Los Angeles
giá vé máy bay từ Vancouver về việt nam
Thanks for sharing nice post
ReplyDeleteBest Regards website maintenance consultant
This written piece gives fastidious understanding yet.It’s amazing in support of me to truly have a web site that is valuable meant for my knowledge
ReplyDeletebest UX agencies