jquery21. What are selectors in jQuery and how many types of selectors are there?
To work with an element on the web page, first we need to find them. To find the html element in jQuery we use selectors. There are many types of selectors but basic selectors are:

Name: Selects all elements which match with the given element Name.
#ID: Selects a single element which matches with the given ID
.Class: Selects all elements which match with the given Class.
Universal (*): Selects all elements available in a DOM.
Multiple Elements E, F, G: Selects the combined results of all the specified selectors E, F or G.
Attribute Selector: Select elements based on its attribute value.

22. How do you select element by ID in jQuery?
To select element use ID selector. We need to prefix the id with “#” (hash symbol). For example, to select element with ID “txtName”, then syntax would be,
Hide   Copy Code


23. What does $(“div”) will select?
This will select all the div elements on page.

24. How to select element having a particular class (“.selected”)?
$(‘.selected’). This selector is known as class selector. We need to prefix the class name with “.” (dot).

25. What does $(“div.parent”) will select?
All the div element with parent class.

26. What are the fastest selectors in jQuery?

ID and element selectors are the fastest selectors in jQuery.

27. What are the slow selectors in jQuery?
class selectors are the slow compare to ID and element.

28. How jQuery selectors are executed?
Your last selectors is always executed first. For example, in below jQuery code, jQuery will first find all the elements with class “.myCssClass” and after that it will reject all the other elements which are not in “p#elmID”.
Hide   Copy Code

$(“p#elmID .myCssClass”);

29. Which is fast document.getElementByID(‘txtName’) or $(‘#txtName’).?
Native JavaScipt is always fast. jQuery method to select txtName “$(‘#txtName’)” will internally makes a call to document.getElementByID(‘txtName’). As jQuery is written on top of JavaScript and it internally uses JavaScript only So JavaScript is always fast.

30. Difference between $(this) and ‘this’ in jQuery?
this and $(this) refers to the same element. The only difference is the way they are used. ‘this’ is used in traditional sense, when ‘this’ is wrapped in $() then it becomes a jQuery object and you are able to use the power of jQuery.
Hide   Copy Code


In below example, this is an object but since it is not wrapped in $(), we can’t use jQuery method and use the native JavaScript to get the value of span element.
Hide   Copy Code


Written by 

Leave a Reply

Your email address will not be published. Required fields are marked *