Tuesday, June 8, 2010

jQuery: By examples

I have been playing around with jQuery from last few days. And I AM LOVING IT. Well still I am in the learning phase, but I thought lets share what I have learnt so far. And the best way to share the knowledge is though EXAMPLES. So here is the first set of examples:

Example: In this example I will flip a box.

Demo: Click on the box to flip it

eM pliF
Filp Me





APIs used in the example (this section is also implemented through jQuery)

  • ID Selector ("#id")
    Select an element based on the id value. For id selectors, jQuery uses the JavaScript function document.getElementById(), which is extremely efficient. When another selector is attached to the id selector, such as h2#pageTitle, jQuery performs an additional check before identifying the element as a match.
  • :first Selector
    Selects the first matched element. The :first pseudo-class is equivalent to :eq(0). It could also be written as :lt(1). While this matches only a single element, :first-child can match more than one: One for each parent.
  • :last Selector
    Selects the last matched element. Note that :last selects a single element by filtering the current jQuery collection and matching the last element within it.
  • Mouse Event - click()
    Bind an event handler to the "click" JavaScript event, or trigger that event on an element. The click event is sent to an element when the mouse pointer is over the element, and the mouse button is pressed and released. Any HTML element can receive this event.
  • DOM Removal - remove()
    Remove the set of matched elements from the DOM. We use .remove() when we want to remove the element itself, as well as everything inside it. In addition to the elements themselves, all bound events and jQuery data associated with the elements are removed.
  • DOM Insertion - appendTo()
    Insert every element in the set of matched elements to the end of the target. With .appendTo(), the content precedes the method, either as a selector expression or as markup created on the fly, and it is inserted into the target container.

Click the link to show code fragment




No comments: