Mutation Person
Mutation Person

Reputation: 30520

How do I determine an HTML input element type upon an event using jQuery?

Given the following sample code:

$(document).ready(function(){
    $(":input").blur(function(){
        alert("The input type is:" );  //How would this look??????
    })
});

How can I detemine whether this is an input, select, text, etc?

This is not a real-world example but should suffice for the purposes of this question

Upvotes: 3

Views: 2944

Answers (5)

Ryan
Ryan

Reputation: 6866

This should work...

$(document).ready(function(){
    $("input").blur(function(){
        var type = this.type;
        alert("The input type is:" + type);
    })
});

Upvotes: 0

BalusC
BalusC

Reputation: 1109635

How can I deteminedetermine whether this is an input, select, text, etc?

Note that select, textarea, "etc" elements are not covered by $('input'). You probably rather want to use $(':input') to get them all.

$(document).ready(function(){
    $(':input').blur(function(){
        alert('The tag is:' + this.tagName);
        if (this.tagName == 'INPUT') {
           alert("The input type is:" + $(this).attr('type'));
        }
    })
});

Upvotes: 5

Jamiec
Jamiec

Reputation: 136174

Why not go through and see what attribute/property would be most useful?

$(document).ready(function(){
    $("input").blur(function(){
        for (var x in this)
            alert(x + ":" + this[x]);
    })
});

Upvotes: 0

Marius
Marius

Reputation: 58999

$(this).attr("type");

for example:

$(document).ready(function(){
    $("input").blur(function(){
        alert("The input type is:" + $(this).attr("type"));
    })
});

Upvotes: 1

Sampson
Sampson

Reputation: 268462

$(this).attr("type");

See jQuery's Selectors/Attribute documentation for additional information.

Upvotes: 11

Related Questions