All posts by Pramod T P

jQuery eq() And get()

eq() returns the element as a jQuery object. This method constructs a new jQuery object from one element within that set and returns it. That means that you can use jQuery functions on it.

get() return a DOM element. The method retrieve the DOM elements matched by the jQuery object. But as it is a DOM element and it is not a jQuery-wrapped object. So jQuery functions can’t be used.

jQuery $(this) And ‘this’

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.

$(document).ready(function(){
$(‘#spnValue’).mouseover(function(){
alert($(this).text());
});
});
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.

$(document).ready(function(){
$(‘#spnValue’).mouseover(function(){
alert(this.innerText);
});
});

jQuery Selectors

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.
  • Attribute Selector: Select elements based on its attribute value.

Load jQuery Locally When CDN Fails

It is a good approach to always use CDN but sometimes what if the CDN is down (rare possibility though) but you never know in this world as anything can happen.

Below given jQuery code checks whether jQuery is loaded from Google CDN or not, if not then it references the jQuery.js file from your folder.

<script type=”text/javascript” src=”http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js”></script>
<script type=”text/javascript”>
if (typeof jQuery == ‘undefined’)
{
document.write(unescape(“%3Cscript src=’Scripts/jquery.1.9.1.min.js’ type=’text/javascript’%3E%3C/script%3E”));
}
</script>

It first loads the jQuery from Google CDN and then check the jQuery object. If jQuery is not loaded successfully then it will references the jQuery.js file from hard drive location. In this example, the jQuery.js is loaded from Scripts folder.

Load jQuery From CDN

Below is the code to load jQuery from all 3 CDNs.

Code to load jQuery Framework from Google CDN

<script type=”text/javascript”
src=”http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js”></script>

Code to load jQuery Framework from Microsoft CDN

<script type=”text/javascript”
src=”http://ajax.microsoft.com/ajax/jquery/jquery-1.9.1.min.js”>
</script>

Code to load jQuery Framework from jQuery Site(EdgeCast CDN)

<script type=”text/javascript”
src=”http://code.jquery.com/jquery-1.9.1.min.js”>
</script>

jQuery CDN

There are 3 popular jQuery CDNs.

1. Google.
2. Microsoft
3. jQuery.

Advantage of using CDN.

  • It reduces the load from your server.
  • It saves bandwidth. jQuery framework will load faster from these CDN.
  • The most important benefit is it will be cached, if the user has visited any site which is using jQuery framework from any of these CDN