Tag Archives: Javascript

jQuery Caching

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.

$(“#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,

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.

jQuery 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.

$(“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.

jQuery Difference Between event.PreventDefault And event.stopPropagation

event.preventDefault(): Stops the default action of an element from happening.
event.stopPropagation(): Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event.

For example, if there is a link with a click method attached inside of a DIV or FORM that also has a click method attached, it will prevent the DIV or FORM click method from firing.

jQuery Difference Between prop() and Attr()

attr(): Get the value of an attribute for the first element in the set of matched elements. Whereas,.prop(): (Introduced in jQuery 1.6) Get the value of a property for the first element in the set of matched elements.

Attributes carry additional information about an HTML element and come in name=”value” pairs. Where Property is a representation of an attribute in the HTML DOM tree. once the browser parse your HTML code ,corresponding DOM node will be created which is an object thus having properties.

.attr() gives you the value of element as it was defined in the html on page load. It is always recommended to use .prop() to get values of elements which is modified via javascript/jquery , as it gives you the original value of an element’s current state.