3

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

0

5 Answers 5

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

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

Sign up to request clarification or add additional context in comments.

Comments

5

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'));
        }
    })
});

Comments

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

for example:

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

Comments

0

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]);
    })
});

Comments

0

This should work...

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

1 Comment

+1 of course this works, and doesn't waste time wrapping this in $ again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.