Skip to Content

TheBlogReaders.com

Salesforce.com, PHP, MySQL, Javascript, Ajax, Htacces

Jquery Interview questions and Answers For Developers Part-6

Be First!

jquery51. What is the difference between event.stopPropagation and event.stopImmediatePropagation?
event.stopPropagation() allows other handlers on the same element to be executed, while event.stopImmediatePropagation() prevents every event from running. For example, see below jQuery code block.
Hide   Copy Code

$(“p”).click(function(event){
event.stopImmediatePropagation();
});
$(“p”).click(function(event){
// This function won’t be executed
$(this).css(“background-color”, “#f00”);
});

If event.stopPropagation was used in previous example, then the next click event on p element which changes the css will fire, but in case event.stopImmediatePropagation(), the next p click event will not fire.
52. How to check if number is numeric while using jQuery 1.7+?
Using “isNumeric()” function which was introduced with jQuery 1.7.

53. How to check data type of any variable in jQuery?
Using $.type(Object) which returns the built-in JavaScript type for the object.

54. How do you attach a event to element which should be executed only once?
Using jQuery one() method. This attaches a handler to an event for the element. The handler is executed at most once per element. In simple terms, the attached function will be called only once.
Hide   Copy Code

$(document).ready(function() {
$(“#btnDummy”).one(“click”, function() {
alert(“This will be displayed only once.”);
});
});​

55. Can you include multiple version of jQuery? If yes, then how they are executed?
Yes. Multiple versions of jQuery can be included in same page.

56. In what situation you would use multiple version of jQuery and how would you include them?
Well, it is quite possible that the jQuery plugins which are used are dependent on older version but for your own jQuery code, you would like to use newer version. So because of this dependency, multiple version of jQuery may required sometimes on single page.

Below code shows how to include multiple version of jQuery.
Hide   Copy Code

<script type=’text/javascript’ src=’js/jquery_1.9.1.min.js’></script>

<script type=’text/javascript’>
var $jq = jQuery.noConflict();
</script

By this way, for your own jQuery code use “$jq”, instead of “$” as “$jq” refers to jQuery 1.9.1, where “$” refers to 1.7.2.

57. Is it possible to hold or delay document.ready execution for sometime?
Yes, its possible. With Release of jQuery 1.6, a new method “jQuery.holdReady(hold)” was introduced. This method allows to delay the execution of document.ready() event. document.ready() event is called as soon as your DOM is ready but sometimes there is a situation when you want to load additional JavaScript or some plugins which you have referenced.
Hide   Copy Code

$.holdReady(true);
$.getScript(“myplugin.js”, function() {
$.holdReady(false);
});

58. What is chaining in jQuery?
Chaining is one of the most powerful feature of jQuery. In jQuery, Chaining means to connect multiple functions, events on selectors. It makes your code short and easy to manage and it gives better performance. The chain starts from left to right. So left most will be called first and so on.
Hide   Copy Code

​$(document).ready(function(){
$(‘#dvContent’).addClass(‘dummy’);
$(‘#dvContent’).css(‘color’, ‘red’);
$(‘#dvContent’).fadeIn(‘slow’);
});​

The above jQuery code sample can be re-written using chaining. See below.
Hide   Copy Code

​$(document).ready(function(){
$(‘#dvContent’).addClass(‘dummy’)
.css(‘color’, ‘red’)
.fadeIn(‘slow’);
});​

Not only functions or methods, chaining also works with events in jQuery. Find out more here.

59. How does caching helps and how to use caching in jQuery?
Caching is an area which can give you awesome performance, if used properly and at the right place. While using jQuery, you should also think about caching. For example, if you are using any element in jQuery more than one time, then you must cache it. See below code.
Hide   Copy Code

$(“#myID”).css(“color”, “red”);
//Doing some other stuff……
$(“#myID”).text(“Error occurred!”);

Now in above jQuery code, the element with #myID is used twice but without caching. So both the times jQuery had to traverse through DOM and get the element. But if you have saved this in a variable then you just need to reference the variable. So the better way would be,
Hide   Copy Code

var $myElement = $(“#myID”).css(“color”, “red”);
//Doing some other stuff……
$myElement.text(“Error occurred!”);

So now in this case, jQuery won’t need to traverse through the whole DOM tree when it is used second time. So in jQuery, Caching is like saving the jQuery selector in a variable. And using the variable reference when required instead of searching through DOM again.

60. You get “jquery is not defined” or “$ is not defined” error. What could be the reason?

There could be many reasons for this.

You have forgot to include the reference of jQuery library and trying to access jQuery.
You have include the reference of the jQuery file, but it is after your jQuery code.
The order of the scripts is not correct. For example, if you are using any jQuery plugin and you have placed the reference of the plugin js before the jQuery library then you will face this error.

(151)

Previous
Next

Leave a Reply