Hi,
I'd like:
$('.whatever').text(); // = [foo, bar, baz]
$('.whatever').text(','); // = foo,bar,baz
But at the moment $('.whatever').text(','); would set the text in all elements
to ','. I don't think we should change that behaviour because it is the more
complicated to simulate:
Christof Donat wrote:
Hi,
I'd like:
$('.whatever').text(); // = [foo, bar, baz]
$('.whatever').text(','); // = foo,bar,baz
But at the moment $('.whatever').text(','); would set the text in all elements
to ','. I don't think we should change that behaviour because it is the more
Hi,
I did do so, but I think it doesn't help to have functions which have
semantically irritating names. If a function is called exists(), that
indicates, that the function is operating on one or no element, not a
collection (the same problem as with is()).
Oh I'm not saying exists()
Christof Donat wrote:
I also think that text() is problematic as well. It should better return an
Array of strings. If I need what text currently does I can do
$('.whatever').text().join(''). I even could do
$('.whatever').text().join(' - oh my god - '), which is not possible with the
Hi,
Why? For me the sweetest thing about using jQuery has been it's
intuitiveness right out of the box. When I started I just looked at some
initial code samples (fancy API pages weren't around back then or I
didn't know about them) and then was virtually able to 'guess' the
jQuery
Hi,
However, as you said this library is more then just code, it's art. And
this is why I'm emphasizing those new functions: They empower the artist
Perfection is achieved, not when there is nothing more to add, but when there
is nothing left to take away.
(Antoine de Saint-Exupéry)
Don't
Hi,
var element;
if ( (element=$('#someID')[0]) ) {
/* element exists */
} else {
/* damn... */
}
I'd prefer
var e
if( (e=$('#someID')).length ) {
// element Exists
} else {
// element does not exist
}
Usually you whant to know if an Element exists to create it
Hi,
if( ! (e=$('#someID').length) )
e = $('p id=someIDasdf/p').appendto('#myParent');
e.hide();
Oops.
if( ! (e=$('#someID')).length )
e = $('p id=someIDasdf/p').appendto('#myParent');
e.hide();
Just close the parentheses where they should be closed :-)
Christof
Christof: I appreciate your comments. Even more however I would have
appreciated if you'd have taken into consideration why I made the
proposal. I didn't do so because I personally need this functionality,
but because I think both exists() and hasClass() would make the library
more accessible
Hi,
Christof: I appreciate your comments. Even more however I would have
appreciated if you'd have taken into consideration why I made the
proposal.
I did do so, but I think it doesn't help to have functions which have
semantically irritating names. If a function is called exists(), that
Mootools:
includes a hasClass() function:
Prototype:
includes a hasClassName() function:
Someone correct me if I'm wrong, but jQuery does have a hasClass method...
it's just called filter ... and far more powerful because you can use any
jQuery expression (not just classes).
brian,
I agree that filter() is powerful, but it is not a replacement for
hasClass... in fact, as u urself said, it is going to select the elements
and create a jquery object for it and it is unnecessary for such a trivial
check as hasClass... But is() is a replacement for sure... it returns a
On Jul 9, 10:19 am, Felix Geisendörfer [EMAIL PROTECTED] wrote:
I've just been wondering if jQuery has some syntactic sugar for checking
if an element exists. I know the following works:
if ($('#my-element').length) {
//
Just use:
if ( $('p').is('.classNameHere') )
// Do something useful
As others have said before, hasClass is on the books for possible
inclusion in jQuery 1.2.
Karl Rudd
On 7/12/07, Brian Cherne [EMAIL PROTECTED] wrote:
Mootools:
includes a hasClass() function:
Prototype:
hj wrote:
On Jul 9, 10:19 am, Felix Geisendörfer [EMAIL PROTECTED] wrote:
I've just been wondering if jQuery has some syntactic sugar for checking
if an element exists. I know the following works:
if ($('#my-element').length) {
//
jQuery is a language and as such requires you to read at least a bit
of documentation or examples before starting. Maybe .is() and .length
should be more prominently visible in the doc, but I see no point in
adding the .exists() and .hasClass() cruft to the (beautiful) jQuery
code.
-- Fil
But I don't think we disagree at all. I wasn't talking about .get() with no
arguments, but rather .get(n) and .size(), which are just slower synonyms
for [n] and .length.
Yeah I agree with you on that. I just read: 'we should get rid of the
get() function' and freaked : p
-- Felix
if ($('#someID')) {
// something matched the selector
}
See, and there you go making a wrong assumption that beginners are much
more likely to run into. !![] is evaluating to true and so is a jQuery
object that has not matched any items. I'm not blaming you for it, it's
In fact, if you find yourself doing a lot of if(something exists) {
... } else { ...}, you might want to consider trying to move some of
your code into a plugin.
The target audience for an exists() function would be new comers to
jQuery. Those are generally a little scared about writing their
jQuery is a language
It was a library last time I checked ; ).
and as such requires you to read at least a bit
of documentation or examples before starting.
Why? For me the sweetest thing about using jQuery has been it's
intuitiveness right out of the box. When I started I just looked at some
What about
Array.prototype.sort.apply( $('li') );
Not sue if that'll work...
I think that could work, but it's breaking chainability so I think the
array plugin mentioned by Jörn earlier is a better alternative. However
since I all I need is sort(), I actually am fine with using get() that
jQuery is a language
It was a library last time I checked ; ).
yeah, well, it can be many things to many people; we all agree it's
code. I think it's art, too
Why? For me the sweetest thing about using jQuery has been it's intuitiveness
right out of the box.
ok, so next time i want to
ok, so next time i want to code something in jquery i'll just write
$(make coffee) ?
Haha, now I'm questioning your ambition. This is the holy grail of all
programming, it's almost blasphemy to make fun of it ; ).
For me (and I really mean not speaking for everyone) it's more
intuitive if
Anyway I can see your argument about how an exists() function would
be redundant. jQuery has a lot of convenience wrappers for things,
but $.fn.exists = function(){return !!this.length}; would be the
smallest and least functional one. Same is almost true for hasClass
(return
On Jul 9, 2007, at 3:05 PM, Matt Kruse wrote:
On Jul 9, 1:50 pm, Sean Catchpole [EMAIL PROTECTED] wrote:
I believe that learning jquery returns an array like object is more
useful than creating a .exists() function.
IMO, many people look for common sense methods that should exist in
On Jul 9, 2007, at 23:57 , Erik Beeson wrote:
In fact, if you find yourself doing a lot of if(something exists) {
... } else { ...}, you might want to consider trying to move some of
your code into a plugin. Most jQuery functions/plugins already deal
with the if(exists)... part by simply not
You can also do:
if ($('#my-element')[0]) {
}
or
if ($('#my-element').size()) {
}
or if you want the sugary syntax you can declare it yourself:
jQuery.fn.exists = jQuery.fn.size;
Mike
On 7/9/07, Felix Geisendörfer [EMAIL PROTECTED] wrote:
I've just been wondering if jQuery has some
I believe that learning jquery returns an array like object is more
useful than creating a .exists() function.
~Sean
Sean, Mike: I agree with your notion that learning that the jQuery
object is array-like (It'd be cool if it was a real array and .push /
.sort would work on it) is very worthwhile. I knew that when I initially
stumbled across the problem and knew that doing it via '.length' was one
solution. I
Felix Geisendörfer wrote:
It'd be cool if it was a real array and .push / .sort would work on it
That isn't so far fetched. There is the array plugin:
http://dev.jquery.com/browser/trunk/plugins/array
And a push-implementation is missing there but easy to add:
push: function( t ) {
On Jul 9, 1:50 pm, Sean Catchpole [EMAIL PROTECTED] wrote:
I believe that learning jquery returns an array like object is more
useful than creating a .exists() function.
IMO, many people look for common sense methods that should exist in
jQuery, or at least as part of a standard and
Matt Kruse wrote:
As another example, I think .hasClass() should exist, even though you
can do .is(.className) - simply because most people will look for a
method called hasClass rather than reading the docs and eventually
finding that .is() is the correct way to do it. It makes jQuery a
little
Felix, not to worry, there's nothing wrong at all with using .length - and
it is obviously faster than a function call.
In the earliest versions of jQuery, the jQuery object was not an array, but
had a private array object that you accessed using .get(n) and .size(). The
only reason those
On 7/9/07, Jörn Zaefferer [EMAIL PROTECTED] wrote:
I can't find the initial branch of this thread. Could someone repeat
what exists() is supposed to do?
$.fn.exists = function() { return !!this.length; }
~Sean
Felix, not to worry, there's nothing wrong at all with using .length
- and it is obviously faster than a function call.
I figured that by now. I think Matt was much better at explaining why I
think an alternative exists() function is useful - it simply is the most
intuitive thing a new jQuery
There's no reason at all to stick with .get(n) and .size()
now that the array-like jQuery object allows the
simpler and more efficient [n] and .length.
I disagree. Whenever you need to sort the elements in an
ul or something then you'll have to use the Array.sort()
function, so you
On Jul 10, 4:50 am, Sean Catchpole [EMAIL PROTECTED] wrote:
I believe that learning jquery returns an array like object is more
useful than creating a .exists() function.
It seems to me that the most common reason for testing if an element
exists is to use it later, so why not:
var element;
Another idea is that if the seletor doesn't select any elements,
return null (as does getElementById() in that case):
...
but that may not be backward compatible. Of what use is an empty
jQuery object?
An empty jQuery object doesn't break chainability:
$('#foo').hide();
Will hide the
In fact, if you find yourself doing a lot of if(something exists) {
... } else { ...}, you might want to consider trying to move some of
your code into a plugin. Most jQuery functions/plugins already deal
with the if(exists)... part by simply not executing if nothing is
selected. If you really
Felix Geisendörfer wrote:
Felix, not to worry, there's nothing wrong at all with using .length
- and it is obviously faster than a function call.
I figured that by now. I think Matt was much better at explaining why I
think an alternative exists() function is useful - it simply is the most
40 matches
Mail list logo