views:

87

answers:

4

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>
+2  A: 

Change this:

 var c = $("input[name=\'lastName\']");

To:

 var c = $("input[name='lastName']");
 alert(c.type);

...

Alternatively:

 alert($(c).attr('type'));
Sarfraz
Escaping quotes that don't need escaping doesn't really do anything. Both of those lines behave identically.
Max Shawabkeh
+4  A: 

$(c).attr('type')

yanoo
no need to wrap `c` in `$()` since it is already a jQuery object, but +1 for `attr` which is the answer to the question.
David Hedlund
+2  A: 

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'));
Max Shawabkeh
+1  A: 

since you're using jQuery, you can do this:

$( 'input[name="lastName"]' ).attr( 'type' );//returns text

Horia Dragomir