views:

2018

answers:

7

What are the hidden features of jQuery?

See also: Hidden features of javascript

+4  A: 

There is no built in "exists" function for JQuery.. but there should be (and can be)!

http://stackoverflow.com/questions/31044/is-there-an-exists-function-for-jquery

Jeff Atwood
What exactly is wrong with `$(selector).length` ? It returns true, unless nothing matched. `.exists()` is one more character, and a unnecessary function call to replace a quick property lookup
gnarf
nothing... jquery's awesome in that it simplified down a lot of things that used to take many lines of code. jquery's not so awesome in that it's caused some of us to forget basic javascript... kinda like how many people seem to forget basic ansi C after having worked in C++ or Obj-C for awhile
pxl
+5  A: 

While the internal data() function is documented, its uses aren't. It's pretty general-purpose, as it allows you to see the data that jQuery has associated with any given elements.

For example, one such use is to see the actions that jQuery has bound to an element in its event registry, as in this answer.

Adam Bellaire
+1  A: 

Here is some good stuff:

http://james.padolsey.com/javascript/things-you-may-not-know-about-jquery/

Serhat Özgel
+1  A: 

You can set up data in a dialog component

Something like

$("#dialog").dialog({
    "someData":"someData",
    buttons:{
        "Is there some data":function() {
            alert($(this).dialog("option", "someData"));
        }
    }
});
Arthur Ronald F D Garcia
This is handy because it extends to basically everything.
Earlz
+2  A: 

Something I did not know until recently, you can select elements within another element in the DOM by passing a second parameter to the jQuery initializer

<div id="outer">
  <div id="inner"> </div>
</div

the inner div is selected by

$('#outer').find('#inner')
//or shorter:
$('#inner', $('#outer'))
//or even shorter:
$('#inner', '#outer')

Also not at all hidden, but I didn't know until recently that enumerating over a jQuery object returns DOM objects. Therefore, if want to get at the underlying DOM object wrapped inside a jQuery array you just do $('#outer')[0]

George Mauer
As useful as the `$(selector, context)` can be, it internally calls `.find()`. Personally I find when reading code, `$(context).find(selector)` to make more sense than seeing `$(selector, context)`.
gnarf
A: 

jQuery's queue functions can be extremely useful.

Some example uses can be seen here: Can somebody explain jQuery queue to me?

gnarf
A: 

That jQuery is JavaScript.

You can use any and all JavaScript with jQuery.

Casey Hope
it's kind of sad how many people forget this.
GSto