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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With