Welcome to our set of jQuery Questions and Answers for experienced programmers. Hope you have gone through common jQuery questions and answers and found that helpful. Let’s discuss more in-depth questions and answers. Share this articles with your friends and co-workers!
(1) What are the selectors in jQuery? Explain with detailed examples.
Selectors in jQuery allow us to find out HTML element in the DOM.
There are a number of selectors in jQuery. The most used once are :
- .Class: which Selects all elements which match with Class specified.
- Name: Selects all elements that match with the given element Name.
- Universal (*): which Selects all elements available within the DOM.
- Attribute Selector: which Selects elements based on its attribute value.
- #ID: Selects a single element which matches with the ID specified
(2) Give examples of widely jQuery CDN?
There are three widely used free CDNs available for jQuery.
(3) Why do we prefer to use CDN for jQuery?
There are major advantage of using CDN. CDN is Content Delivery Network.
– If you use CDN, means, you are reducing your own server load.
– Using external CDN will save you bandwidth. jQuery framework will load faster from these CDN since they are one of the biggest and fasted CDN available in the world.
– The most important advantage is it could be cached on client end (your website visitor), if the user has previously visited any site that is using jQuery framework from any of these CDNs available. The CDNs are widely used anyways, which increases probability of this case.
(4) What is the best practice to make sure loading jQuery out of CDN is not failed. If failed, there is an alternate way to load jQuery library in that case?
This is definitely a great question. Normally we have not seen CDN failed loading jQuery but there is a chance. To make sure there is a failsafe condition to this and load it directly from your server.
First make sure to save jQuery library to your server location. e.g. within the “Scripts” directory. File name should be the same as you downloaded. e.g. jquery.2.1.3.min.js.
So the path becomes Scripts/jquery.2.1.3.min.js
Now you want to write logic to confirm whether CDN URL works perfectly fine or not. and if that URL fails to load properly, you want to call the file that is saved on your server. Use code below to accomplish this functionality.
if (typeof jQuery == ‘undefined’)
(5) What is the difference between event.PreventDefault() and event.stopPropagation() in jQuery?
– event.preventDefault() is used to stop the default action of an element from executing at runtime.
– event.stopPropagation() is used to prevent the event from bubbling up the DOM tree, which means preventing any parent handlers from being notified of the event.
For instance, if there is a link with a click method attached inside of a DIV or FORM that also has a click method attached; using event.stopPropagation() will prevent the DIV or FORM click method from firing at the execution time.