Patrioticcow
Patrioticcow

Reputation: 27038

how to check var's in jquery

i have this script:

var display = '<div class="uploadData" id="pxupload'+ itr +'_text">';

and i want to check if the div from diplay exists. Something like this:

if (display = null){
        $("#px_display").append(display);
    }else if  (display != null) {
        $(config.buttonClear).trigger('click');
    }

I don't know if the statement is correct.

Upvotes: 0

Views: 298

Answers (2)

alex
alex

Reputation: 490243

Best you could do is...

var display = '<div class="uploadData" id="pxupload'+ itr +'_text">';

var id = $(display).attr('id');

if ($('#' + id).length) {
   // Some element with the same id attribute exists.
}

This, of course, is not too accurate.

It would be better if you made that variable first, like so...

var displayId = 'pxupload' + itr;

if ($('#' + displayId).length) {
   // Some element with the same id attribute exists.
}

Upvotes: 0

szeliga
szeliga

Reputation: 621

since the ID will be unique and constructible, can you do something like:

if($("#pxupload" + itr + "_text") == null){
        $("#px_display").append(display);
}else if  (display != null) {
    $(config.buttonClear).trigger('click');
}

Upvotes: 1

Related Questions