How to get type of input object [duplicate]
Possible Duplicate:
How do I determine an HTML input element type upon an event using jQuery?
I want to get type of input object but I alway got "undefined".
What is missing it this code?var c = $("input[name=\'lastName\']");
c.val("test");
alert(c.type);
My form
<form action="formAction.jsp" method="post">
FirstName <input type="text" name="firstName" id="firstName"></input><br/>
LastName <input type="text" name="lastName"></input><br/>
Address <input type="text" name="address"></input><br/>
Tel <input type="text" name="tel"></input><br/>
Email <input type="text" name="email"></input><br/>
</form>
$(c).attr('type')
jQuery does not return an ordinary element object, which has its attributes as properties. It returns a jQuery wrapper; you can access its attributes using attr()
, so what you want is:
alert(c.attr('type'));
Change this:
var c = $("input[name=\'lastName\']");
To:
var c = $("input[name='lastName']");
alert(c.type);
...
Alternatively:
alert($(c).attr('type'));
since you're using jQuery, you can do this:
$( 'input[name="lastName"]' ).attr( 'type' );//returns text
精彩评论