Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the type of a control using Javascript and JQuery

I am trying to use JQuery to get the type of control and following is the code that I am using.

$('#selCity').attr('type')

where selCity is of type select. When I try the above code it returns as undefined but when I use the alternative code with Javascript, it returns the correct type.

Please look into this fiddle to understand it clearly: http://jsfiddle.net/Ye8e9/

Could someone advice on how I can acheive this correctly using JQuery? Is this an issue with JQuery or am I making a mistake?

like image 438
Abishek Avatar asked Nov 21 '12 12:11

Abishek


2 Answers

Use

$('#selCity').prop('type')

As of jQuery 1.6, the .attr() method returns undefined for attributes that have not been set. In addition, .attr() should not be used on plain objects, arrays, the window, or the document. To retrieve and change DOM properties, use the .prop() method.

Reference

DEMO

like image 79
Sibu Avatar answered Sep 25 '22 12:09

Sibu


if you mean the type of tag, the use this

 $("#selCity").get(0).tagName

See your demo here

like image 22
Raab Avatar answered Sep 22 '22 12:09

Raab