Just to throw in my 2cents/pence worth...
In the early versions of jQuery we could use $('#id').is() to return
true if the array contains any values. This stopped working in more
recent versions. If you like that solution it could be resurrected by
enhancing the is() method in your code using
PROTECTED] On
Behalf Of McLars
Sent: Thursday, December 27, 2007 5:24 PM
To: jQuery (English)
Subject: [jQuery] Re: check if an id exists
Yeah, it dawned on me later that the chaining/no error thing was
behind the empty array. I think the no errors bit is overhyped,
though. Other than the $('#id').hide
I believe you are picturing a static web page though. In my case, my
application is a single page that get's heavily modified on the fly as
it is used. I'm adding and removing elements to my page at various
times. These elements have their own behaviors that need to be applied.
So it is
Josh Nathanson wrote:
well as many other jQuery newbies? Specifically, what signifigance
does jQuery's $() being like an array have, and even when
you use an
ID selector?
It is very significant -- in fact you could even say it is
one of the most important code-saving aspects of
In the end, it isn't in the spirit of things to add a core function $
('#someid').exists() when $('#someid').is('*') happens to do the
job already (and it's shorter too!).
When you're minifying to 10kb, you don't want redundancy.
It might sound strange, but (for me) it is actually pretty rare
Other than the $('#id').hide() example, you aren't going to
get very far when a selection fails, and it will make debugging a
little weird if you don't throw an error at some point.
What is erroneous about selecting no elements?
Here is an example of using chaining to significantly reduce
Mike, I think you've hit the nail on the head. It isn't intuitive that
jQuery always returns an array-like object. One assumes that a failed
search returns nothing. That leads me to question why it returns the
empty array. Shouldn't it return nothing if nothing is found?
Larry
Michael Geary
Michael Geary write:
It's interesting that people don't immediately think of using
$('#id').length 0 (with or without the 0). It tells me
that they're not aware of a fundamental fact about jQuery:
The $() function always returns an array-like object that has
.length and [0..n]
.
-- Josh
- Original Message -
From: Mike Schinkel [EMAIL PROTECTED]
To: jquery-en@googlegroups.com
Sent: Thursday, December 27, 2007 5:23 AM
Subject: [jQuery] Re: check if an id exists
Michael Geary write:
It's interesting that people don't immediately think of using
$('#id').length 0
It isn't intuitive that jQuery always returns an array-like object.
If jQuery didn't return a jQuery object, you would not be able to
chain methods. Chaining is at the core of jQuery's design. In many
cases you don't even need to check if an id exists, since any
following methods in the chain
Yeah, it dawned on me later that the chaining/no error thing was
behind the empty array. I think the no errors bit is overhyped,
though. Other than the $('#id').hide() example, you aren't going to
get very far when a selection fails, and it will make debugging a
little weird if you don't throw an
I would definitely recommend $(#id).length. When working in a
language, it's important to keep in mind the language's idioms. In the
case of JS, 0 == false, so if($(#id).length) is a perfectly good
idiom.
It's a bit confusing coming from another language (where 0 is true),
but that's
It's funny, if I saw:
if( $('#id').is('*') ) { ... }
I would have no clue what the code was trying to do until I thought hard
about it: Let's see... is star... Now that's going to match *anything*.
Wouldn't it always return true? Naw, that can't be right, what would be the
point of this code...
extendify!
$(document).ready(function() {
jQuery.fn.exists = function() {
return ( this.is('*') )
}
}
});
On Dec 27, 10:28 am, Michael Geary [EMAIL PROTECTED] wrote:
It's funny, if I saw:
if( $('#id').is('*') ) { ... }
I would have no clue
Yehuda, I will agree and (respectfully) disagree with you. Yes, using
the length property is expedient. It's how millions of programmers
have been doing such things, in many different languages, and they
will probably continue to do it. No harm there, as indeed JS treats
zero as a false and it
Hamish, I like that!
Michael, the question is number two on the FAQ. Despite what seasoned
programmers may think, it's obviously not intuitive to newbies.
Personally, I think it's always good to point out alternatives to
learners. The .is() method is actually quite versatile and can be used
to
Yep... my code have size(). Forgot to point it...
And thanks for info about more efficient way - using length.
So... which way is better than?
1. $('#id').length 0
2. $('#id').length() !== 0
3. $('#id').is('*') //never try it... but look pretty
- Alexey
On Dec 25, 2007 5:39 AM, Michael Geary
$('#id').length is the old school, and most widely used, technique.
This is probably the fastest.
$('#id').is('*') does make sense semantically (expresses the intent),
and is more flexible.
Larry
On Dec 25, 12:33 am, Alexey Blinov [EMAIL PROTECTED] wrote:
Yep... my code have size(). Forgot
not sure, this is untested but might work(not sure what jQuery return if
nothing is found)
if($('#id'))
{
//it exists
}
else
{
//it does not
}
debussy007 wrote:
Hi,
what is the best way in jquery to check wether an id exists ?
Thank you for any kind help !!
--
View this
I tried :
if( ! $('#tooltip') ) {
...
}
it gives me a javascript error if I add this :(
Eridius wrote:
not sure, this is untested but might work(not sure what jQuery return if
nothing is found)
if($('#id'))
{
//it exists
}
else
{
//it does not
}
debussy007 wrote:
(the error is elem has no properties in the jquery js)
debussy007 wrote:
I tried :
if( ! $('#tooltip') ) {
...
}
it gives me a javascript error if I add this :(
Eridius wrote:
not sure, this is untested but might work(not sure what jQuery return if
nothing is found)
From:
http://docs.jquery.com/Frequently_Asked_Questions#How_do_I_test_whether_an_element_exists.3F
if ( $('#theId').length ) {
// exists
} else {
// doesn't exist
}
- Richard
On Dec 24, 2007 9:11 AM, debussy007 [EMAIL PROTECTED] wrote:
Hi,
what is the best way in jquery to check
Thanks this works great !!
Richard D. Worth-2 wrote:
From:
http://docs.jquery.com/Frequently_Asked_Questions#How_do_I_test_whether_an_element_exists.3F
if ( $('#theId').length ) {
// exists
} else {
// doesn't exist
}
- Richard
On Dec 24, 2007 9:11 AM, debussy007 [EMAIL
That won't work because $() *always* returns a valid jQuery object even if
there are no matching elements. This allows you to write code like
$('.foo').hide() without worrying about whether there are any elements with
class=foo or not.
Instead (as pointed out elsewhere in the thread), check the
Personally I do it with `$('#id').size !== 0` but length is goood to i think
:)
On Dec 24, 2007 5:55 PM, Cloudream [EMAIL PROTECTED] wrote:
$('#id').length0 ?
On Dec 24, 10:11pm, debussy007 [EMAIL PROTECTED] wrote:
Hi,
what is the best way in jquery to check wether an id exists ?
Well, just to split hairs, the length property returns a number, not a
true boolean. You could also use:
$('*').is('#myId') or you can reverse it like so: $
('#myId').is('*')
The .is() method does return a boolean. An advantage is that you can
apply this to a subset of elements, for
$('#id').length may not be a boolean value, but $('#id').length0 is.
However, in the contexts where you're likely to use this, it doesn't matter:
if( $('#id').length ) ...
or:
$('#id').length ? ... : ...
In either of those cases, the length property works fine for the test,
regardless of
$('#id').size !== 0 would not work. I'll bet that your actual code has
.size() instead of .size without the parentheses, right? The size property
is a method, so the !== 0 test would always return true (since a function
reference is never equal to 0).
.length is slightly more efficient than
28 matches
Mail list logo