How to determine the type of HTML input element on an event using jQuery?

Given the following code example:

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

How can I determine if this is input, selection, text, etc.?

This is not an example of the real world, but for the purposes of this issue it should be sufficient

+3
source share
5 answers
$(this).attr("type");

See the jQuery Selectors / Attribute documentation for more information .

+11
source

How can I determine if this is input, selection, text, etc.?

Please note that elements of select, textarea, "etc" is not covered $('input'). You probably 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'));
        }
    })
});
+5
source
$(this).attr("type");

:

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

...

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

, / ?

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

Source: https://habr.com/ru/post/1726246/


All Articles