Thursday, May 17, 2012

jQuery Event Functions


The jQuery event handling methods are core functions in jQuery.
Event handlers are method that are called when "something happens" in HTML. We term it as "triggered (or "fired") by an event". 
It is a common practice to put jQuery code into event handler methods in the <head> section as the code in the head section is available for the complete document as compared to the body section as code in body section will be available only for the elements after that point of code  :

Example:

<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("button").click(function(){
    $("p").hide();
  });
});
</script>
</head>

<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Click me</button>
</body>
</html>

In the example above, a function is called when the click event for the button is triggered Means when the User will Click on the Button:
$("button").click(function() {..some code... } )
The method hides all <p> elements:
$("p").hide();

Functions In a Separate File
As we have already discussed that we can put the jQuery functions in a separate file too with extension ".js". Let me explain this by a scenario suppose our website contains a lot many pages, and as other languages here with our jQuery we want functions to be easy to maintain and reused, then we should  put our jQuery functions in a separate .js file.
When we use jQuery here, the functions are added directly into the <head> section, However, sometimes it is preferable to place them in a separate file, like this (remember our other css and java script files with the src attribute):

<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="my_jquery_functions.js"></script>
</head>

Before moving further with event here I would like to discuss a little about Name Confilcts
jQuery Name Conflicts
jQuery uses the $ sign as a shortcut for jQuery.
Some other JavaScript libraries also use the dollar sign for their functions.
The jQuery noConflict() method specifies a custom name (like jq), instead of using the dollar sign.

Example

<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
var jq=jQuery.noConflict();
jq(document).ready(function(){
  jq("button").click(function(){
    jq("p").hide();
  });
});
</script>
</head>

<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Click me</button>
</body>
</html>


In Next article I will Discuss more Events Stay tuned.


Tuesday, May 8, 2012

jQuery Selector : More Examples


We were discussing jQuery Selectors in my previous post so here I am extending that post and defining few more examples commonly used in web development

Some More Examples of jQuery Selector
Syntax
Description
$(this)
Current HTML element
$("p")
All <p> elements
$("p.myobj")
All <p> elements with class="myobj"
$("p#myobj")
All <p> elements with id="myobj"
$("p#myobj:first")
The first <p> element with id="myobj"
$(".myobj")
All elements with class="myobj"
$("#myobj")
The first element with id="myobj"
$("ul li:first")
The first <li> element of the first <ul>
$("ul li:first-child")
The first <li> element of every <ul>
$("[href$='.jpg']")
All elements with an href attribute that ends with ".jpg"
$("div#myobj .head")
All elements with class="head" inside a <div> element with id="myobj"

Selector
Example
Selects
*
$("*")
All elements
#id
$("#lastname")
The element with id=lastname
.class
$(".myobj")
All elements with class="myobj"
element
$("p")
All p elements
.class.class
$(".myobj.demo")
All elements with the classes "myobj" and "demo"



:first
$("p:first")
The first p element
:last
$("p:last")
The last p element
:even
$("tr:even")
All even tr elements
:odd
$("tr:odd")
All odd tr elements



:eq(index)
$("ul li:eq(3)")
The fourth element in a list (index starts at 0)
:gt(no)
$("ul li:gt(3)")
List elements with an index greater than 3
:lt(no)
$("ul li:lt(3)")
List elements with an index less than 3
:not(selector)
$("input:not(:empty)")
All input elements that are not empty



:header
$(":header")
All header elements h1, h2 ...
:animated
$(":animated")
All animated elements



:contains(text)
$(":contains('W3Schools')")
All elements which contains the text
:empty
$(":empty")
All elements with no child (elements) nodes
:hidden
$("p:hidden")
All hidden p elements
:visible
$("table:visible")
All visible tables



s1,s2,s3
$("th,td,.myobj")
All elements with matching selectors



[attribute]
$("[href]")
All elements with a href attribute
[attribute=value]
$("[href='default.htm']")
All elements with a href attribute value equal to "default.htm"
[attribute!=value]
$("[href!='default.htm']")
All elements with a href attribute value not equal to "default.htm"
[attribute$=value]
$("[href$='.jpg']")
All elements with a href attribute value ending with ".jpg"
[attribute^=value]
$("[href^='jquery_']")
All elements with a href attribute value starting with "jquery_"



:input
$(":input")
All input elements
:text
$(":text")
All input elements with type="text"
:password
$(":password")
All input elements with type="password"
:radio
$(":radio")
All input elements with type="radio"
:checkbox
$(":checkbox")
All input elements with type="checkbox"
:submit
$(":submit")
All input elements with type="submit"
:reset
$(":reset")
All input elements with type="reset"
:button
$(":button")
All input elements with type="button"
:image
$(":image")
All input elements with type="image"
:file
$(":file")
All input elements with type="file"



:enabled
$(":enabled")
All enabled input elements
:disabled
$(":disabled")
All disabled input elements
:selected
$(":selected")
All selected input elements
:checked
$(":checked")
All checked input elements

jQuery Selectors


jQuery Selectors
Till now we have learnt how to select different HTML elements. It is a key point to learn how jQuery selects exactly the elements you want to apply an effect to.
Note: jQuery selectors allow you to select HTML elements (or groups of elements) by element name, attribute name or by content.
jQuery Element Selectors
jQuery uses CSS selectors to select HTML elements.
$("p") selects all <p> elements.
$("p.intro") selects all <p> elements with class="intro".
$("p#demo") selects all <p> elements with id="demo".

jQuery Attribute Selectors
jQuery uses XPath expressions to select elements with given attributes.
$("[href]") select all elements with an href attribute.
$("[href='#']") select all elements with an href value equal to "#".
$("[href!='#']") select all elements with an href attribute NOT equal to "#".
$("[href$='.jpg']") select all elements with an href attribute that ends with ".jpg".

jQuery CSS Selectors
jQuery CSS selectors can be used to change CSS properties for HTML elements.
The following example changes the background-color of all p elements to red:
$("p").css("background-color","red");

Previous Posts:
1: jQuery Syntax
2: Adding jQuery
3: Introduction to jQuery

Subscribe via email

Enter your email address:

Delivered by FeedBurner

MSDotnetMentor

MSDotnetMentor My Website http://msdotnetmentor.com

Blog Archive