Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
64
rated 0 times [  69] [ 5]  / answers: 1 / hits: 41772  / 12 Years ago, tue, march 20, 2012, 12:00:00

Unfortunately I don't have access to JQuery and all it's nicety. But I do have access to JavaScript. How do I check if an OPTION exist in a HTML Select?



EDIT: To clarify, I need to know if an option exist. So for example:



<select>
<option>Volvo</option>
<option>Saab</option>
<option>Mercedes</option>
<option>Audi</option>
</select>


I check if Hyndai is an OPTION, and it is not.


More From » html

 Answers
11
document.getElementById(myselect).options[0] //accesses first option via options[]


would select the first option in your select. If it fails you know that there are no options in your select. If you get data by appending .value after the .options[0] it's not empty. Without javascript you will not be able to achieve this. Only HTML does not deliver the functionality you want.



for (i = 0; i < document.getElementById(myselect).length; ++i){
if (document.getElementById(myselect).options[i].value == Hyndai){
alert(Hyndai is available);
}
}

[#86720] Monday, March 19, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
susanap

Total Points: 413
Total Questions: 82
Total Answers: 99

Location: South Georgia
Member since Mon, Oct 19, 2020
4 Years ago
;