Thursday 29 May 2014

Second Day: Selectors of jQuery

jQuery selectors are backbone or you can say the most important parts of the jQuery library. jQuery selectors are very need to perform any action on any element or event.

jQuery Selectors 

jQuery selectors allow you to select HTML element(s) and then perform required operation on them.

jQuery selectors are used to "search" (or select) HTML elements based on their name, id, class, types, attributes, values of attributes and much more. It's based on the idea of CSS Selectors as its known and easy to use, and it has some of its own selectors as addition, which can be used with other selectors.

All selectors in jQuery start with the dollar sign and parentheses: $() or jQuery()

As '$' sign is alias of jQuery so you can always find examples with jQuery() or $() depending on your version of jQuery.


Element Selector

This jQuery selector selects HTML elements (tags) based on their element (tag) name.
You can select all <div> elements (tag) on a HTML page like this:
$("div")
Example Code: This below code will hide all <div> elements, when button is clicked.

$(document).ready(function(){
  $("button").click(function(){
    $("div").hide();
  });
}); 


The #id Selector

This jQuery selector selects single unique element with its id attribute of HTML tag, since id can be applied to any element in HTML, this selector plays a great role.

An id is supposed to be unique within a page, so you should use the #id selector when you want to find a single, unique element.
To find an element with a specific id, just write a hash character, followed by the id value of the HTML element you want to search or select, like one below:

$('#main')

Example Code: This below code will hide element with id "main", when button is clicked.

$(document).ready(function(){
  $("button").click(function(){
    $("#main").hide();
  });
});
Working example link


The .class Selector

 This jQuery selector selects element by their specified class name, which are defined as attributes in HTML elements. An Element can have multiple classes separates by spaces, and jQuery class selector can select element based on one or more of those classes of element.

To search or select element using class you need to write period (.) character followed by class name, like one given below:


$('.row')
Example Code: This below code will hide all elements with class "row", when button is clicked.

$(document).ready(function(){
  $("button").click(function(){
    $(".row").hide();
  });
});
 Working example link


Miscellaneous jQuery Selectors

There are other selectors like pseudo selectors, grouping selectors, nesting selector, etc. I am mentioning some of them in list below:

Syntax Description
$("*") Selects all HTML elements
$(this) Selects the current HTML element object
$("h1, p") Selects all <h1> and <p> elements
$("#main, .row") Selects element with id="main" and elements with class="row"
$("div.row") Selects all <div> elements with class="row"
$("div:first") Selects the first <div> element
$("ul li:first") Selects the first <li> element of the first <ul>
$("ul li:first-child") Selects the first <li> element of every <ul>
$("[src]") Selects all elements with an src attribute
$("input[type='text']") Selects all <input> elements with a type attribute value equal to "text"
$("input[type!='button']") Selects all <input> elements with a type attribute value NOT equal to "button"
$(":button") Selects all <button> elements and <input> elements of type="button"
$("div:even") Selects all even ordered <div> elements
$("div:odd") Selects all odd ordered <div> elements

 

For More info on jQuery selectors write to me or refer CSS selectors.

Monday 5 May 2014

First Day: Basics and Introduction with jQuery

Hi all,

Its my very first post so pardon me.

I have been working with jquery for past three years and found guys having difficulty in understanding it, so I though why not help others with it.

This is official home page of jQuery and you can download jquery library from jQuery download.

I would recommend having minified version of jQuery to use, since its small in size will save user time as well.

Or you may use some CDN (Content Delivery Network for this work) as per your need.

There are two versions of jQuery available for downloading:
  • Production version - this is for your live website because it has been minified and compressed
  • Development version - this is for testing and development (uncompressed and readable code)

you may include jquery in your code as given below: 
<script src="//code.jquery.com/jquery-1.9.1.min.js"></script>
OR
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
OR
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.0.min.js"></script> 
you may choose any version based on your need. I have worked on jquery version 1.9.1, so I would be following that in my code examples.

First thing I would like you to tell is jQuery is JavaScript library. When I say library that means it have almost functions which would save your work, it greatly simplies JavaScript programming. And its easy to learn just work around is needed.

Before you start with jQuery you need to have basic knowledge about these things:

1- HTML (HyperText Markup language) - Why?
     We would work with HTML for jQuery.

2- CSS (Cascading Style Sheet) - Why?
     We would use many selectors from CSS.

3- JavaScript - Why?
     Since jQuery is library of JavaScript we would need to understand basics of JavaScript.


What is jQuery?

jQuery is a lightweight, "write less, do more", JavaScript library with cross browser compatibility support, i.e., it will work same across all browsers including IE earlier versions like IE 6.

The purpose of jQuery is to make it much easier to use JavaScript on your website.

jQuery takes a lot of common tasks that require many lines of JavaScript code to accomplish, and wraps them into methods that you can call with a single line of code.

jQuery also simplifies a lot of the complicated things from JavaScript, like AJAX(Asynchronous JavaScript and XML ) calls and DOM manipulation.

There are lots of plugin created using jQuery library, which are being used around internet website some of those plugins you might want to have are:
  • Image slider plugin
  • Pagination plugin
  • Form Validation  plugin
  • etc....

The jQuery library contains the following features:
  • HTML/DOM (Document Object Model) manipulation
  • CSS manipulation
  • HTML event methods
  • Effects and animations
  • AJAX (Asynchronous JavaScript and XML)
  • Utilities


Why jQuery?

There are lots of other JavaScript frameworks out there, but jQuery seems to be the most popular, and also the most extendable.
Many of the biggest companies on the Web use jQuery.


How jQuery works?

With jQuery you select (query) HTML elements and perform "actions" on them.

jQuery Syntax

The jQuery syntax is tailor made for selecting HTML elements and performing some action on the element(s).
Basic syntax is: $(selector).action()
  • A '$' sign to define/access jQuery
  • A (selector) to "query (or find)" HTML elements
  • A jQuery action() to be performed on the element(s)
  • '$' sign is alias of jQuery.
Examples:
$(this).hide() - hides the current element.
$("p").hide() - hides all <p> elements.
$(".test").hide() - hides all elements with class="test".
$("#test").hide() - hides the element with id="test".

Are you familiar with CSS selectors?

jQuery uses CSS syntax to select elements. You will learn more about the selector syntax in the next post of my blog.

The Document Ready Event

You might have noticed that all jQuery methods are inside a document ready event:

$(document).ready(function(){

   // jQuery methods go here...

}); 

This is to prevent any jQuery code from running before the document is finished loading (is ready).
It is good practice to wait for the document to be fully loaded and ready before working with it. This also allows you to have your JavaScript code before the body of your document, in the head section.
Here are some examples of actions that can fail if methods are run before the document is fully loaded:
  • Trying to hide an element that is not created yet
  • Trying to get the size of an image that is not loaded yet
Tip: The jQuery team has also created an even shorter method for the document ready event:

$(function(){

   // jQuery methods go here...

}); 

Use the syntax you prefer. We think that the document ready event is easier to understand when reading the code.

$(document).ready(function(){
  $("button").click(function(){
    $("p").hide();
  });
}); 


Working Example: Demo link

you can try these above function to check if your jQuery is working fine.

Happy Coding!!!