Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
76
rated 0 times [  80] [ 4]  / answers: 1 / hits: 105324  / 14 Years ago, tue, january 18, 2011, 12:00:00

I want to get the type of element, that I've got selected using jQuery selectors.



Markup:



<select name=a></select>
<input name=b type=text />
<textarea name=c></textarea>


Javascript:



var field_names = new Array(
'a',
'b',
'c'
);

for(var i = 0; i < field_names.length; i++) {
var field = $('[name=' + required_fields[i] + ']:visible');

// ?????
// What do I write here to get one of those outputs:
// Element with name a is <select>
// Element with name b is <input>
// Element with name c is <textarea>

alert('Element with name ' + required_fields[i] + ' is ' + element_type);
}

More From » jquery

 Answers
32

Simple:



var element_type = '<' + field.get(0).tagName.toLowerCase() + '>';


In a nutshell, this retrieves the DOM element associated with field and gets its tag name via the tagName attribute inherited from DOMElement, then transforms the result to lowercase using String's toLowerCase() method. Some browsers will return the tagName in uppercase, so for consistency you should transform it to lower case.


[#94162] Monday, January 17, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
luna

Total Points: 698
Total Questions: 114
Total Answers: 93

Location: Israel
Member since Wed, Apr 14, 2021
3 Years ago
;