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();
    }
});
1

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

0

You could just say:

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

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

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.