0

Suppose I have created one jQuery function to check if elements exist or not like:

jQuery.fn.exists = function () { 
    return this.length > 0; 
}

Then I call:

if ($("#MyDiv").exists() == false)
   alert('not exist');
else
   alert('exist');

If I call jQuery function above it works. But can't we call the jquery function like this way exists('#MyDiv') ? If I try to call this way then I am not getting result...why?

gdoron
  • 147,333
  • 58
  • 291
  • 367
Thomas
  • 33,544
  • 126
  • 357
  • 626
  • 1
    Unless you have defined a function `exists`, you cannot call it (obviously?). Of course you can easily define one which accepts a selector and performs the same operation as you defined in `$.fn.exists`. Or am I missing something? – Felix Kling Jun 19 '12 at 18:43
  • @FelixKling. What all of we're missing, what this function is good for... – gdoron Jun 19 '12 at 18:44
  • @gdoron: I agree with you that such a method is rather unnecessary in this particular case, but the question still stands (imo) apart from what the method is doing. – Felix Kling Jun 19 '12 at 18:45
  • @Thomas, Are you interested in the `exist` function only, or are you asking a more broad question? – gdoron Jun 19 '12 at 18:48

4 Answers4

2

ARRG, please, don't use this useless exist function!

It can be simply with:

if ($('#MyDiv').length)
    // Exist
else
    // Doesn't exist.

No plugins needed, everyone knows what this code does, Don't use exist.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
gdoron
  • 147,333
  • 58
  • 291
  • 367
  • I don't think that's point of the question. I assumed `exist` was just an example. – gen_Eric Jun 19 '12 at 18:44
  • 2
    @Rocket. I don't think so, look at [this question](http://stackoverflow.com/q/31044/601179) see how many people like that useless function. – gdoron Jun 19 '12 at 18:46
  • "Syntactic sugar causes cancer of the semicolon." -Alan Perlis – gen_Eric Jun 19 '12 at 18:50
  • 1
    @Rocket. Actually the main problem with a lot of the jQuery programmers, they have **ZERO** knowledge in javascript, so they didn't thought about `if($(...).length)` instead of `$(...).length > 0` – gdoron Jun 19 '12 at 18:52
  • @gdoron: There is nothing wrong with being explicit (`> 0`). This can make the code easier to understand. But I guess this is rather a matter of style. `if($(...))` is worse ;) – Felix Kling Jun 19 '12 at 18:55
  • 1
    @FelixKling, I expect every programmer ever touching\reading my real code to know about truthy-falsy nature of javascript values, so `> 0` is just noise. IMO. – gdoron Jun 19 '12 at 18:57
1

Try like below,

$.exists = function (selector) {
    return $(selector).length > 0;
}

and use as,

$.exists('#test') 

DEMO: http://jsfiddle.net/skram/hgaPt/1/

Please use this in a bigger picture, using it for simple thing as exist is just a overkill.

Selvakumar Arumugam
  • 79,297
  • 15
  • 120
  • 134
  • why u did not specify jquery function prototype like jQuery.fn.exists = function () {) rather u wrote code like $.exists = function (selector) {)....both are same? jQuery.fn keyword is optional. please discuss – Thomas Jun 20 '12 at 07:01
0
exists('#MyDiv')

This won't work because you didn't make a function called exists. You made a function called jQuery.fn.exists (jQuery.fn is the prototype of jQuery objects).

For exists('#MyDiv') to work, you need to make a function called exists.

function exists(sel){
    return $(sel).length;
}
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
0

$.fn is equal to $.prototype, so indirectly using $.fn you are hooking your functions to jQuery object prototype, so the selector needs to be passed to the jQuery object not the chained function name.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Ajay Beniwal
  • 18,857
  • 9
  • 81
  • 99