jQuery Basics
jQuery Introduction
The files needed to run a jQuery function can be downloaded from the URL mentioned below:
There are actually two versions of the file that is available, the first one id the minified and compressed version, jquery-1.10.1.min.js, which is basically used for the production environment and the other is the full version, uncompressed and readable, which is used for the development environment.
If somebody does not want to download the file, they can use it from the CDN (Content Delivery Network) provided both by Google and Microsoft, the details are here under:
- Google - http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js
- Microsoft - http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.1.min.js
Note: If you look at the Google URL above - the version of jQuery is specified in the
URL (1.10.1). If you would like to use the latest version of jQuery, you can
either remove a number from the end of the version string (for example 1.10),
then Google will return the latest version available in the 1.10 series (1.10.0,
1.10.1, etc.), or you can take it up to the whole number (1), and Google will
return the latest version available in the 1 series (from 1.1.0 to 1.10.1).
jQuery Syntax
The general syntax is $(selector).action(); where
- $ is to access the jQuery
- selector is to find/query HTML elements
- action() is to a jQuery action to be performed on the elements
$(document).ready(function() {
// jQuery code here});
jQuery Selectors
There are many different ways by which one can select/query an element, some of the types that are there are as follows:
- element selector - $("p")
- id selector - $("#btnSubmit")
- class selector - $(".cssclass")
- all elements - $("*")
- current element - $(this)
- particular element with a class - $("p.cssclass")
- first matching element - $("p:first")
A list of other selectors can be found here http://www.w3schools.com/jquery/jquery_ref_selectors.asp
jQuery Event Methods
Every element that is there in the DOM an event can be attached to it, it can be said that an event is a response to the action done by the user on a particular page element. For e.g.
$("p").click(function() {
});
A list of other event methods can be found here http://www.w3schools.com/jquery/jquery_ref_events.asp
jQuery DOM (Document Object Model) Manipulation
The various methods that are available for the manipulation of the DOM are as follows:
- $("#btnShow").click(function() {
alert($("#lblName").text());
});
This will return the text displayed within the element - $("#btnShow").click(function() {
alert($("#lblName").html());
});
This will return the HTML code for the text displayed within the element - $("$btnShow").click(function() {
alert($("txtName").val());
});
This will return the value within the input elements - $("#btnShow").click(function() {
alert($("a").attr("href"));
});
This will allow the user to get the attributes defined within an element
A list of other DOM methods can be found here http://www.w3schools.com/jquery/jquery_ref_html.asp
jQuery Traversing
By traversing means moving through the DOM elements from a single point it can be either up, down, left or right. The various methods that are available are as follows:
- $("span").parent();
This returns the direct parent of the element - $("span")parents();
This returns all the parents of the element till the root element of the document - $("span").parents("ul");
This returns all the parents of the element which are of the ul type - $("span").parentUltil("div");
This returns all the parents for the element until the element div - $("div").children();
This returns all the direct child of the element - $("div").children("p.class");
This returns all the direct child of the element using the css class named - $("div").find("span");
This will search and return all the child elements starting with the span tag - $("div").siblings();
This returns all the sibling elements of the element - $("div").next();
This returns the next sibling of the element - $("div").nextAll();
This returns all the siblings next to the element - $("div").nextUntil("p");
This returns all the siblings of the element till the p element - $("div").first("p");
This returns the first p tag under the div - $("div").last("p");
This returns the last p tag in the div - $("p").eq(0);
This return the p tag with the specified index number - $("p").filter(".intro");
This will return the p tag with the class intro - $("p").not(".intro");
This will return all the p tags that do not use the intro class
A list of the other DOM methods can be found here http://www.w3schools.com/jquery/jquery_ref_traversing.asp
jQuery No Conflict Method
There might be a situation whereby some other third party software might also be using the same $ sign, in that case there will be a conflict, in order to avert such a situation we can use the method "noConflict", when called we will not be able to use the $ sign any more and will be needed to use the syntax "jQuery" instead.
$.noConflict();
jQuery(document).ready(function() {
});
Comments
Post a Comment