Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
77
rated 0 times [  81] [ 4]  / answers: 1 / hits: 31515  / 12 Years ago, tue, july 10, 2012, 12:00:00

In Javascript, given the id of an element (in a String format), how can I check if the id of the element refers to a drop down list element, or a text input element?



The function should return true if the id refers to a drop down list (<select>) element, or a text input element, and false otherwise.


More From » html

 Answers
3

Try using:
document.getElementById('idNameGoesHere').tagName



So the function could be:



function isSelectOrTextField(idName) {
var element = document.getElementById(idName);
if(element.tagName === 'SELECT') {return true;}
if(element.tagName === 'INPUT' && element.type === 'text') {return true;}
return false;
}


You could expand this to check for <textarea> as well.



EDIT :
Or choose jbabey's answer as it's using nodeName and is better formatted.



apparently nodeName has wider browser support.


[#84352] Monday, July 9, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
gerardob

Total Points: 571
Total Questions: 115
Total Answers: 96

Location: Cyprus
Member since Mon, Oct 24, 2022
2 Years ago
;