jQuery: eq() vs. get()

There are several ways to access queried DOM elements. You get either a DOM element using get() or a jQuery object using eq(). This can be confusing while you try to call jQuery API functions on DOM elements.

The example shows how to get the first array element on three different ways.

<ul>
  <li></li>
  <li></li>
  <li></li>
</ul>

<script type="text/javascript">
    $(document).ready(function(){
        var elems, elem;
        elems = $("li"); // get an array of all the list items

        // This is a jQuery object that you can call jQuery related methods on.
        elem = elems.eq(0); 

        // This is the DOM element itself that lets you access the native
        // javascript attributes and methods.
        elem = elems.get(0); 

        // This is the DOM element too.
        elem = elems[0];
    });
</script>

Instead of using the filter method eq() you can combine the filter with the selector like this

var elem = $("li:eq(0)");

or

var elem = $("li:first");

To turn a DOM element into a jQuery object you can wrap it with jQuery(element) or $(element) if you’re not in conflict mode. But keep in mind that this creates a new object which wastes consumes memory so pay attention if you do this in a loop.

DeliciousDiggTechnorati FavoritesRedditLinkedInFacebookSpurlTwitterWebnewsYiGGMySpaceYahoo BookmarksFriendFeedGoogle BookmarksLiveJournalShare

jQuery injection using Firebug?

A new day, a new question.

I was trying to load a json object while using Firebug and jQuerify to inject jQuery code into the website of the currently active tab. If you don’t know what I’m talking about, these are firefox add-ons. Firebug is a great debugging tool whereas jQuerify adds the jQuery library to any website if it’s not yet loaded.

Now some details to my recent problem. If I enter the URL into the browsers location field I get a proper json object back. If I send an XMLHttpRequest to the same URL the server responses with the status code 301 Moved Permanently. Has this issue to do with the Same Origin Policy (SOP) ?

If so I need a workaround because this URL will be the new API URL for some web apps.

Any suggestions what this could be?

Edit:
It wasn’t a SOP problem but I also had to face with such a problem later. The above described behaviour came from a missing / at the end of the URL. Entering such a URL into the location field the browser automatically add the tailing slash if it’s missing. After I found that out I wasn’t able to fetch the json from another domain. It’s been months that I’ve been coding jQuery a lot so I forgot one little but important information. The server has to send an additional header to allow other domains. More about this later in a seperate post.

DeliciousDiggTechnorati FavoritesRedditLinkedInFacebookSpurlTwitterWebnewsYiGGMySpaceYahoo BookmarksFriendFeedGoogle BookmarksLiveJournalShare

Changing the menu text with jQuery

If you need to toggle the menu text client-side when you click it some JavaScript is required. This is best done with a framework like jQuery. All you need is an event handler bound to the menu item. It uses a counter variable to store the state which will be checked for oddness. If it’s odd use the one otherwise the other text.

Show only defect entries

<script type="text/javascript">
  jQuery(document).ready( function(){
    var flip = 0;
    jQuery('#owad_show_defects').bind( "click", function(e){
      flip++;

      if ( flip % 2 == 0 )
        jQuery('#owad_show_defects').text('Show all entries');
      else
        jQuery('#owad_show_defects').text('Show only defect entries');

    });
  });
</script>
DeliciousDiggTechnorati FavoritesRedditLinkedInFacebookSpurlTwitterWebnewsYiGGMySpaceYahoo BookmarksFriendFeedGoogle BookmarksLiveJournalShare