AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



html-jquery: How to check whether a html id attribute is dropdown?

i want to check that an id attribute of html tag is a dropdown or not in jquery?

html x 24
jquery x 12
Posted On : 2015-11-15 14:35:47.0
profile Rishi Kumar - anyforum.in Rishi Kumar
523188245674
up-rate
4
down-rate

Answers


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:
-------------------------------







Posted On : 2015-11-15 15:55:15
Satisfied : 1 Yes  0 No
profile Garima Gupta - anyforum.in Garima Gupta
596129554554
Reply This Thread
up-rate
5
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in