2

I try to detect if some exists in :

var listBoxSelector = $("div#selectors select:eq(1)");
var option = listBoxSelector.find("option[value=" + value + "]:eq(0)");
alert(option);

But if such option not exists I receive Object in any case. What is the better way to detect is with some value is exists in or not?

Dmytro Zarezenko
  • 10,308
  • 9
  • 56
  • 99

4 Answers4

2

You can use the length property of the jQuery object:

The number of elements in the jQuery object.

if (listBoxSelector.find("option[value="+value+"]").length) {
     alert('Exists')
}
Ram
  • 140,563
  • 16
  • 160
  • 190
1

Check the length of the option object:

var listBoxSelector = $("div#selectors select:eq(1)");
var option = listBoxSelector.find("option[value=" + value + "]:eq(0)");
if (option.length>0){
   alert(option);
}
Curtis
  • 98,395
  • 62
  • 265
  • 345
0
if(listBoxSelector.find("option[value=" + value + "]:eq(0)") == 0){
  //nothing found
}
Slawomir Pasko
  • 813
  • 8
  • 14
0

I answered this same question with the following plugin here. Please visit answer for full details on creating of plugin.


The following plugin would allow you to use a callback feature (staying inline with jQuery style markup) if the element exist. So for your example, you might do something like:

listBoxSelector.exist(function() { // will ONLY fire if this element even exist
    alert($(this).find("option[value=" + value + "]:eq(0)"));
})

Plugin

(function($) {
    if (!$.exist) {
        $.extend({
            exist: function() {
                var ele, cbmExist, cbmNotExist;
                if (arguments.length) {
                    for (x in arguments) {
                        switch (typeof arguments[x]) {
                            case 'function':
                                if (typeof cbmExist == "undefined") cbmExist = arguments[x];
                                else cbmNotExist = arguments[x];
                                break;
                            case 'object':
                                if (arguments[x] instanceof jQuery) ele = arguments[x];
                                else {
                                    var obj = arguments[x];
                                    for (y in obj) {
                                        if (typeof obj[y] == 'function') {
                                            if (typeof cbmExist == "undefined") cbmExist = obj[y];
                                            else cbmNotExist = obj[y];
                                        }
                                        if (typeof obj[y] == 'object' && obj[y] instanceof jQuery) ele = obj[y];
                                        if (typeof obj[y] == 'string') ele = $(obj[y]);
                                    }
                                }
                                break;
                            case 'string':
                                ele = $(arguments[x]);
                                break;
                        }
                    }
                }

                if (typeof cbmExist == 'function') {    //  has at least one Callback Method
                    var exist =  ele.length > 0 ? true : false; //  strict setting of boolean
                    if (exist) {    // Elements do exist
                        return ele.each(function(i) { cbmExist.apply(this, [exist, ele, i]); });
                    }
                    else if (typeof cbmNotExist == 'function') {
                        cbmNotExist.apply(ele, [exist, ele]);
                        return ele;
                    }
                    else {
                        if (ele.length <= 1) return ele.length > 0 ? true : false;
                        else return ele.length;
                    }
                }
                else {  //  has NO callback method, thus return if exist or not based on element existant length
                    if (ele.length <= 1) return ele.length > 0 ? true : false; //   strict return of boolean
                    else return ele.length; //  return actual length for how many of this element exist
                }

                return false; //    only hits if something errored!
            }
        });
        $.fn.extend({
            exist: function() {
                var args = [$(this)];
                if (arguments.length) for (x in arguments) args.push(arguments[x]);
                return $.exist.apply($, args);
            }
        });
    }
})(jQuery);

jsFiddle

Community
  • 1
  • 1
SpYk3HH
  • 21,961
  • 10
  • 67
  • 81