The $('#id'), or jQuery('#id'), function returns a special jQuery
"collection" (which acts much like an array in some ways). It does not
return an actual DOM node.

jQuery is a library that helps you "select things" (via CSS selectors)
and "do stuff" (usually "all at once") to those things.

To check if there was any DOM node "selected" use:

  var o = $('#myid');
  if (o.length != 0) {
    // do something
  }

To get the first DOM node out of a jQuery collection use:

  var theNode = $('#myid')[0];

To select multiple objects pass in a string of selectors (just like in CSS):

  var myNodes = $('#checkInDate-Deal, #checkOutDate-Deal');

To change an attribute of the selected nodes you can either use the
attr() method:

  myNodes.attr('value', 'mm/dd/yyyy');

Or in this case, there is a shortcut method:

  myNodes.val('mm/dd/yyyy');

Something to watch out for is that the each() method (when called "on"
a jQuery "collection" object) actually returns the "raw" DOM nodes,
one at a time. ie:

  myNodes.each(function() {
    // this == a raw DOM node
  });

Karl Rudd

On Tue, Nov 25, 2008 at 12:13 PM, halcyonandon <[EMAIL PROTECTED]> wrote:
>
> Hi,
>
> I'm trying change all instances of "document.getElementById()" with
> the jQuery's DOM traversal syntax, however it breaks it everytime.
>
> A simple example is this function:
>
>    rm: function(){
>    var m = document.getElementById('discountMonth');
>    if(m)
>        m.selectedIndex = 0;
>    }
>
> If I change "document.getElementById('discountMonth');" to "$
> ('#discountMonth');"  or any variation of that it breaks.(I tried
> single and double quotes as well as attempting to get the element by
> class and so forth with no luck).
>
> The HTML looks like this <select onchange="$.bots.rd();return false;"
> id="discountMonth" name="deals[discountMonth]"
> class="input_deals_dropdown"><option value=""/></select>.
>
> The more troublesome issue is with a function like this
>
>    rd: function (){
>    var ds = new Array('checkInDate-Deal', 'checkOutDate-Deal');
>    $(ds).each(function(i){
>        el = document.getElementById(ds[i]);
>        if(el)
>            el.value = 'mm/dd/yyyy';
>    });
>    }, ...
>
> With the HTML looking like "<input type="text" onblur="$.bots.rm
> ();return false;" id="checkOutDate-Deal" value="mm/dd/yyyy"
> name="checkOutDate" class="input_deals"/>
>
> Any help understanding jQuery's DOM traversal especially with regard
> to array iterations would be much appreciated, thanks.
>
>

Reply via email to