Hi All,

Thanks for the responses.

Firstly, everyone is assuming I am referring to an array. Not
necessarily the case. In fact in a legit XHTML 1.0 Strict document an
id is supposed to be unique is it not? My example used the # to refer
to a unique id on the page, therefore *not* an array of objects.

Secondly my question is, in part, not so much about the worth, speed,
friendliness of this code, but whether it's clearly documented.

Perhaps I am merely experiencing the simplicity of jQuery's design and
expecting it to extend further than it can? Perhaps not.

I don't understand (and I am more than willing to suggest it might be
my lack of understanding here that is the issue) why Example A (below)
and Example B (more efficient but same example) should work, but
Example C does not.

Example A

document.getElementById('foo').selectedIndex;
document.getElementById('foo').nodeName;
document.getElementById('foo').className;
document.getElementById('foo').nodeType;
document.getElementById('foo').blahProperty;
document.getElementById('foo').exampleProperty;

Example B

var fooObject = document.getElementById('foo');

fooObject.selectedIndex;
fooObject.nodeName;
fooObject.className;
fooObject.nodeType;
fooObject.blahProperty;
fooObject.exampleProperty;

Example C

$('#foo').selectedIndex;
$('#foo').nodeName;
$('#foo').className;
$('#foo').nodeType;
$('#foo').blahProperty;
$('#foo').exampleProperty;

Please don't jump on the properties I've picked, they are just taken
from Firebug and merely illustrative.

AFAIK all three examples get an element on the page as a *single* (not
an array) object.

I think it's reasonable (though perhaps not programmatically correct)
to see $('#foo') as the equivalent of document.getElementById('foo').
If this is not true in jQuery, which it does not appear to be, all I
am saying is this distinction should be clearly documented.

In an ideal world, I think it would rock to have a syntax like this:

$('#foo').anyProperty;      // for normal 'old school' DOM properties
$('#foo').jQueryMethod(); // for jQuery methods

However I'm not pretending to know the level of JS that the great Mr
John Resig knows. If the ideal world syntax is not possible or if
people don't like it, fair enough. Again my point is that there may be
a need for this topic to be documented, that's all I'm saying.
Personally I'll probably never forget again after this experience!

pd


On Aug 16, 3:43 am, "Michael Geary" <[EMAIL PROTECTED]> wrote:
> > From: pd
>
> > I've been hacking with jQuery on and off lately and I've now
> > hit the annoying problem of not being able to access simple
> > DOM 0 properties unless, apparently, using either of the
> > following syntaxes:
>
> > $('#foo')[0]
>
> > $('#foo').get(0)
>
> pd, just to help clarify... The $() function returns an array of DOM
> elements. You access elements of an array, of course, by using [n].
>
> I don't know if I would call this an "annoying problem", it's just the way
> it is.
>
> Imagine this code:
>
>    var myArray = [
>       document.getElementById('foo'),
>       document.getElementById('goo')
>    ];
>
> Now you have an array of two elements, [0] and [1], and each element is a
> reference to a DOM element.
>
> If you want the first element of the array, you would use:
>
>    myArray[0]
>
> And you could access DOM properties with:
>
>    alert( myArray[0].id );  // alerts 'foo'
>
> Or you could do:
>
>    var firstElement = myArray[0];
>    alert( firstElement.id );  // alerts 'foo'
>
> What you couldn't do:
>
>   alert( myArray.id );  // alerts undefined
>
> After all, myArray is not a DOM element, it's an *array* of DOM elements.
>
> A jQuery object returned by $() is no different. Whenever you see $(), think
> "array of DOM elements".
>
> (Well, it is slightly different - it is not an actual JavaScript Array
> object, but it does have .length and [n] properties so it works like an
> array for these purposes.)
>
> -Mike

Reply via email to