user3162341
user3162341

Reputation: 251

How can i check if the input does not exist

I want to check if this input doesn't exist.

I have this condition for checking if the input exist but i want to do the opposite.

if ($('input[name="urlpdfvcmd"]')) {
alert('Input exist.');
}

Thanks.

Upvotes: 1

Views: 1322

Answers (4)

cesar andavisa
cesar andavisa

Reputation: 340

you can try to do a prev undefined check :

var input = $('input[name="urlpdfvcmd"]');

    if( typeof input === 'undefined') {
       alert('input not exist')
    }

then you can do :

    if (!$('input[name="urlpdfvcmd"]').length || $('input[name="urlpdfvcmd"]') === "") {
        alert('Input not exist.');
    }

Upvotes: 0

Snehal S
Snehal S

Reputation: 875

You can check visibility of that input with "visible" attribute

For example,

if ($('input[name="urlpdfvcmd"]').is(':visible')) {
    alert('Input exist.');
}

Upvotes: 0

Kaushik
Kaushik

Reputation: 2090

I know it's been a long time since the question was asked, but I found the check to be clearer like this :

if ($("#A").is('[myattr]')) {
    // attribute exists
} else {
    // attribute does not exist
}

(As found on this site here)

EDIT

if ($('#A').attr('myattr')) {
    // attribute exists
} else {
    // attribute does not exist
}

The above will fall into the else-branch when myattr exists but is an empty string or "0". If that's a problem you should explicitly test on undefined:

if ($('#A').attr('myattr') !== undefined) {
    // attribute exists
} else {
    // attribute does not exist
}

Upvotes: 1

mr. Pavlikov
mr. Pavlikov

Reputation: 1012

If no length, input does not exist:

if (!$('input[name="urlpdfvcmd"]').length) {

actually existance should be checked with length aswell, not the way you've done it:

if ($('input[name="urlpdfvcmd"]').length) {

Upvotes: 3

Related Questions