It seems more intuitive and consistent than:
$('#foo')[0].className;
$('#foo')[0].size;
$('#foo')[0].type;
If you're repeatedly accessing the first DOM object of a jQuery
selector array, it's a good practice to set a variable:
var myObject = $('#foo')[0];
..then you can access DOM
On Aug 15, 11:29 am, pd [EMAIL PROTECTED] wrote:
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]
Will throw error if there is
The point is to get the first DOM node, so you'd still need to do [0], which
would still throw an error if there was no match. If you're not certain that
there'll be a match, maybe something like this would be safer:
var $foo = $('#foo');
if($foo.length 0) {
var foo = $foo[0];
} else {
//
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]
Will throw error if there is no match, IIRC.
No, it won't. It is
On Aug 16, 8:55 pm, Michael Geary [EMAIL PROTECTED] wrote:
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]
Will
$('#foo')[0]
Will throw error if there is no match, IIRC.
No, it won't. It is not an error to fetch a nonexistent
array element
Thanks for a nice explanation. I'm sorry for jumping
without reading it properly.
Glad to help - but no apology needed! You should see some of
var a = $(...);
var b = a.length;
a.XXX();
var c = a.get(0);
Now, 'a' is a jQuery object that contains all of the elements that matched
the selector. 'b' is the number of elements found. The third line is calling
a jQuery function or plugin on the jQuery object that is wrapping the
selected
Just curious, how exactly do you expect to be able to access a 0
object?
Are you thinking you can go: object.0.function()?
If so, regardless of how cool jQuery is, when an array's key uses
numerics, you simpy cannot access it as an object because objects must
start with an alpha character. I dont
gradez28 wrote:
Just curious, how exactly do you expect to be able to access a 0
object?
Are you thinking you can go: object.0.function()?
If so, regardless of how cool jQuery is, when an array's key uses
numerics, you simpy cannot access it as an object because objects must
start with an alpha
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 $()
From: Erik Beeson
The [n] syntax is just a shortcut for
.get(n):http://docs.jquery.com/Core#get.28_num_.29
From: Gordon
I would imagine that the [] syntax would be a bit faster than the
get() syntax, on the grounds that the former is simply
addressing an array, whereas the latter
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
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.
Wrong, it *is* still an array of objects, it's just an array of length
1. Do console.log($('#foo')) and you'll see that it is still an array,
and an array with
Thanks Eric
I'll remember to consider jQuery selector results an array from now
on.
So, do you feel like updating the wiki? Sounds like you would be the
best person to do so as your understanding appears quite deep.
I had a look at Mr Willison's article yesterday but only got half way
through
Uh, thanks, you flatter me, but I'm not one of the people that edit the
wiki. I think it would be sweet if the web team got in touch with Simon and
asked him if we could pull that article into docs.jquery.com as an official
jQuery Primer. I think it focuses on the right introductory aspects of
Futher to this overall topic Eric, do you think it would be possible/
wise to implement a jQuery method that returns any of the standard DOM
properties? I'm just wishlisting but I think this syntax (each line a
different example property):
$('#foo').dom('className');
$('#foo').dom('size');
http://docs.jquery.com/DOM/Attributes#Attr
--Erik
On 8/15/07, pd [EMAIL PROTECTED] wrote:
Futher to this overall topic Eric, do you think it would be possible/
wise to implement a jQuery method that returns any of the standard DOM
properties? I'm just wishlisting but I think this syntax
17 matches
Mail list logo