7

The code does work below when the access to the webpage, it automatically hide #OrderDeliveryAddress div. But I am wondering is this correct way doing it?

Is there a way to check if .selectAddressList div/class exist first and then check the value?

$(document).ready(function() {
    if ($(".selectAddressList").val() == "selectAddressBook") {
         $("#OrderDeliveryAddress").hide();
    }
});
user622378
  • 2,318
  • 6
  • 42
  • 63
  • 2
    possible duplicate of [jQuery - how to check if an element exists?](http://stackoverflow.com/questions/5293041/jquery-how-to-check-if-an-element-exists) or [Check if element exists](http://stackoverflow.com/questions/4795928/check-if-element-exists) and probably a billon others... – Felix Kling Apr 27 '11 at 17:49

4 Answers4

8

Personally I would use:

if ($(".selectAddressList").length > 0)

This checks if the jQuery object has any items, in other words if anything matched the selector you passed in.

justkt
  • 14,610
  • 8
  • 42
  • 62
3

if($(".selectAddressList").length > 0)

At a second glance though, you're using a class selector for this - do you have multiple items using this class on the page? If so, you might run into conflicts there as you're checking the .val() of it/them. If not, you might consider using element id as opposed to class.

Demian Brecht
  • 21,135
  • 5
  • 42
  • 46
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:

$(".selectAddressList").exist(function() { // with NO PARAM, will ONLY fire if element exist
    /*  DO WORK     */
})  //  notice, this maintains "chainability", so you could make more calls on this element

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
  • 22,272
  • 11
  • 70
  • 81
0

You could just say:

if ($(".selectAddressList").length)

since 0 would mean false in this case and everything else would evaluate to true.

amurra
  • 15,221
  • 4
  • 70
  • 87