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);
}
Simple:
In a nutshell, this retrieves the DOM element associated with
fieldand gets its tag name via thetagNameattribute inherited fromDOMElement, then transforms the result to lowercase usingString‘stoLowerCase()method. Some browsers will return thetagNamein uppercase, so for consistency you should transform it to lower case.