You can use nodeName in javascript to get the tag name. and on the basis of that you can determine if an id attribute of a html tag is dropdown or not. See the below example:
index.html:
----------------------------------
<!DOCTYPE HTML>
<html>
<head>
<title>Check if HTML id type is dropdown example-anyforum.in</title>
</head>
<body>
<div id="form">
<form>
<select name="country" id="country">
<option>Select a country</option>
<option>India</option>
<option>Nepal</option>
<option>Bhutan</option>
<option>Pakistan</option>
<option>China</option>
</select>
<input type="text" />
<textarea rows="1" cols="15"></textarea>
<button type="button" id="check" onclick="checkId()">Check HTML ID Type- Click Here</button>
</form>
</div>
<script type="text/javascript">
function checkId(){
var element=document.getElementById(´country´);
if(element.nodeName==´SELECT´){
alert("This is dropdown");
}
else if(element.nodeName==´INPUT´ && element.type==´text´){
alert("This is textfield");
}
else if(element.nodeName==´TEXTAREA´){
alert("This is textarea");
}
else{
alert("Could not be determined");
}
}
</script>
</body>
</html>
Note: If you are including jquery on your page, you can bind the click event on the button id as follows:
<!DOCTYPE HTML>
<html>
<head>
<title>Check if HTML id type is dropdown example-anyforum.in</title>
</head>
<body>
<div id="form">
<form>
<select name="country" >
<option>Select a country</option>
<option>India</option>
<option>Nepal</option>
<option>Bhutan</option>
<option>Pakistan</option>
<option>China</option>
</select>
<input type="text" />
<textarea rows="1" cols="15" id="country"></textarea>
<button type="button" id="check">Check HTML ID Type- Click Here</button>
</form>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(´#check´).click(function(){
if($(´#country´).is(´select´)){
alert(´This is dropdown.´);
}
else if($(´#country´).is(´textarea´)){
alert(´This is textarea.´);
}
else if($(´#country´).is(´input:([type="text"])´)){
alert("This is textfied.");
}
else{
alert("Could not be determined.");
}
});
</script>
</body>
</html>
Output of above example: -------------------------------
Select a country
India
Nepal
Bhutan
Pakistan
China
Check HTML ID Type- Click Here
5