Showing posts with label jQuery: By Examples. Show all posts
Showing posts with label jQuery: By Examples. Show all posts

Monday, June 28, 2010

jQuery By Examples: Load data through Ajax - 2

In this example, I am populating a Drop Down list asynchronously through the .load() method. Advantage of loading data through .load() method is that it allows us to specify a portion of the remote document to be inserted. This is achieved with a special syntax for the url parameter. If one or more space characters are included in the string, the portion of the string following the first space is assumed to be a jQuery selector that determines the content to be loaded. Therefore instead of loading the complete document, here I can load specific portion of the document. In this example first I am getting the list of all the Tags available under the Index section of the page. This is achieved through the special URL syntax used in the .load() method. The next setp is to load the data in a temporary div. Here is the sample data available in the div element:

<li>
<a dir='ltr' href='http://myerrorslog.blogspot.com/search/label/Application%20Server%20FAQs'>Application Server FAQsa>
<span dir='ltr'>(1)span>
li>
<li>
<a dir='ltr' href='http://myerrorslog.blogspot.com/search/label/Articles'>Articlesa>
<span dir='ltr'>(1)span>
li>

Once the data is loaded in the div, I am populating the drop down by creating option element dynamically. Every option has following attributes:
text - text from the a tag
value - href from the a tag

Once all the li elements are process, the temp div is removed.

Now when a user, clicks the button to get the posts for the selected Tag, data is loaded again asynchronously through the .load() method. Instead of loading the complete post, I am loading specific part of the post.

Demo:








Click the link to show code fragment



Tuesday, June 22, 2010

jQuery: By Examples - Link Preview

About the example:
When a user hovers over a link, sneak preview of the link pops up. And when user hovers over a thumbnail, original size image pops up.

Link preview has been implemented through Ajax.load() function, where data for the link is loaded from one of the older posts. To know more about Ajax.load() function checkout
jQuery: By examples - Load data through Ajax


Demo:


Click the link to show code fragment



Monday, June 21, 2010

jQuery: By examples - Load data through Ajax -1

 This is another example in the series of jQuery: By Examples. In this example I will load data from one of the posts. Instead of loading complete post (including title) I will load data from a specific portion of the page. Following functions/methods are used in this example:
  1. .load( url, [ data ], [ complete(responseText, textStatus, XMLHttpRequest) ] )
  2. .hide()
  3. .appendTo()
  4. slideDown()
.load() method is the simplest way to get data from the server. Due to browser security restrictions, most "Ajax" requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, or protocol. Using the special syntax for url, you can get data from specif portion of the remote document. If the url has one or more spaces, the portion of the string following the first space is assumed to be a jQuery selector that determines the content to be loaded. For example:
  • .load('http://myerrorslog.blogspot.com/2006/12/introduction.html div[class="post-body entry-content"]')
In this case I am fetching data from the DIV element whose CLASS attribute is equals to post-body entry-content,  in the remote document - http://myerrorslog.blogspot.com/2006/12/introduction.html.

Demo:

Click the link to show code fragment



Wednesday, June 9, 2010

Recursive call through arguments.callee

Is it possible for an anonymous function to refer itself in javascript?
When I read the question for the first time even I was totally blank. The very next thought came to my mind was "Is it really possible?".
Well the answer is YES. It is possible for an anonymous function to refer itself through the use of arguments.callee.
arguments is a local variable for a function object. callee is one of the properties of arguments. arguments.callee allows anonymous function to refer to itself, which is necessary for recursion. Here is an example:

Example: In this example, I will use arguments.callee within an anonymous function which is used as a callback for show() and hide() methods.

Demo:

Show the API list

  • 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.
  • show(duration, [ callback ] )
    Display the matched elements. When a duration is provided, .show() becomes an animation method. The .show() method animates the width, height, and opacity of the matched elements simultaneously. If supplied, the callback is fired once the animation is complete.
  • hide(duration, [ callback ] )
    Hide the matched elements. When a duration is provided, .hide() becomes an animation method. The .hide() method animates the width, height, and opacity of the matched elements simultaneously. When these properties reach 0, the display style property is set to none to ensure that the element no longer affects the layout of the page. If supplied, the callback is fired once the animation is complete.

Click the link to show code fragment



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