MikeBrody
MikeBrody

Reputation: 317

How do I check the type of my JQuery variable?

I'm trying to execute a function on the condition that the input triggering it isn't of type submit.

$(document).ready(function() {

//triggers the capture() function whenever any input on the page loses focus
$("input").blur(function() {

// This is the part that's not working. I need to check if the specific input that 
// triggered the code to run is of type submit. 

if (("input").type == 'submit') {
    alert("this worked");
}
else {

    capture();

}
});

});

Right now capture() is being called even when I blur an input of type submit. I want this code to trigger the first conditional statement when an input of type submit is what triggers the outer function to run.

Upvotes: 1

Views: 406

Answers (2)

Jonathan Lonowski
Jonathan Lonowski

Reputation: 123453

jQuery will assign the value of this inside the event callbacks to be the element that the event was triggered for, so you can access its properties with that:

if (this.type === 'submit') {
    // ...
} else {
    // ...
}

(At least up until another function is involved, as that'll have its own this value.)

The condition you have currently is testing the property of the string, "input", which will likely be undefined and not equal to 'submit'.


Though, if you just want to exclude submit buttons entirely, you could also use the selector for that using :not() and jQuery's custom :submit selectors.

$('input:not(:submit)').blur(capture);

Upvotes: 6

Lu Roman
Lu Roman

Reputation: 2260

Use the attr method with 'type' like this

if($(this).attr('type') === 'submit'){

}

Upvotes: 3

Related Questions