user2486937
user2486937

Reputation:

Check if a selector is not

I have a noob question about jQuery selector. I want to wrap an element after a click, but only if it has a specific class name (".js-loader"), and only if it's an input[type"submit"], or an input[type="button"], or a or if it got a class name ".form-button" ...

var selector = '.js-loader';

$(selector).on('click', function() {
    if( $(this).is(???) ) {
        console.log('Its a submit button or something like that, so its ok.');
    }

    else {
        console.log('You shall not pass.')
    }
});

The problem is, I'm a true novice in javascript and jQuery and I just have no idea how to doing this properly.

Thanks for your help, and sorry for my english :)

Edit : I want to put those selector in another variable, i'll need them later.

http://jsfiddle.net/fMUc9/

Upvotes: 0

Views: 77

Answers (5)

Arun P Johny
Arun P Johny

Reputation: 388316

To the is() function pass the selectors against which you want to test the element against

$(selector).on('click', function() {
    if( $(this).is('input[type="submit"], input[type="button"]') ) {
        console.log('Its a submit button or something like that, so its ok.');
    }
    else {
        console.log('You shall not pass.')
    }
});

Upvotes: 2

Ringo
Ringo

Reputation: 3965

Try this:

$(selector).click(function() {
    if( $(this).is("input[type=button], input[type=button], a") || $(this).className == 'form-button' ) {
        console.log('Its a submit button or something like that, so its ok.');
    }

    else {
        console.log('You shall not pass.')
    }
});

Upvotes: 0

Mr.G
Mr.G

Reputation: 3559

Try this:

var selector = '.js-loader';

$(selector).on('click', function() {
   if( $(this).is('input[type"submit"], input[type="button"],.form-button') ) {
        console.log('Its a submit button or something like that, so its ok.');
    }

    else {
        console.log('You shall not pass.')
    }
});

Upvotes: 0

Roman Pushkin
Roman Pushkin

Reputation: 6079

Try this:

$(selector).on('click', function() {
    if( $(this).is('input :submit, input :button, a.form-button') ) {
        console.log('Its a submit button or something like that, so its ok.');
    }

    else {
        console.log('You shall not pass.');
    }
});

Upvotes: 0

Joke_Sense10
Joke_Sense10

Reputation: 5402

Try this:

$(selector).on('click', function() {
if( $(this).is('input[type="submit"], input[type="button"], a, .form-button') ) {
    console.log('Its a submit button or something like that, so its ok.');
}

else {
    console.log('You shall not pass.')
}
});

Upvotes: 0

Related Questions