0

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>
Community
  • 1
  • 1
wearetherock
  • 3,721
  • 8
  • 36
  • 47

4 Answers4

5

$(c).attr('type')

Jan Dudulski
  • 843
  • 1
  • 8
  • 14
  • 1
    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 Feb 15 '10 at 08:01
2

Change this:

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

To:

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

...

Alternatively:

 alert($(c).attr('type'));
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
2

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
  • 37,799
  • 10
  • 82
  • 91
1

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

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

Horia Dragomir
  • 2,858
  • 24
  • 21