0

I want to make an HTML select drop box with 25 options. After selection I want with a command button to show an alert box showing the type of the selection. Options are divided to three (3) types:

  • Options 1-10 = TYPE1
  • Options 11-20 = TYPE2
  • OPTIONS 21-25 = TYPE3

Below is my html code that does not work...

<select id="item1" name="Item 1">
<OPTION VALUE="0"> Select...</OPTION>
<OPTION VALUE="1">option1</OPTION>
<OPTION VALUE="2"> option2</OPTION>
<OPTION VALUE="3"> option3</OPTION>
<OPTION VALUE="4"> option4 </OPTION>
<OPTION VALUE="5">option5</OPTION>
<OPTION VALUE="6">option6</OPTION>
<OPTION VALUE="7">option7</OPTION>
<OPTION VALUE="8">option8</OPTION>
<OPTION VALUE="9">option9</OPTION>
<OPTION VALUE="10">option10</OPTION>
<OPTION VALUE="11">option11</OPTION>
<OPTION VALUE="12">option12</OPTION>
<OPTION VALUE="13">option13</OPTION>
<OPTION VALUE="14">option14</OPTION>
<OPTION VALUE="15">option15</OPTION>
<OPTION VALUE="16">option16</OPTION>
<OPTION VALUE="17">option17</OPTION>
<OPTION VALUE="18">option18</OPTION>
<OPTION VALUE="19">option19</OPTION>
<OPTION VALUE="20">option20</OPTION>
<OPTION VALUE="21">option21</OPTION>
<OPTION VALUE="22">option22</OPTION>
<OPTION VALUE="23">option23</OPTION>
<OPTION VALUE="24">option24</OPTION>
<OPTION VALUE="25">option25</OPTION>
<OPTION VALUE="25">option26</OPTION>
</select>

<button onclick="message()">Find your Type</button>

<SCRIPT>

function message() {

var s = document.getElementById('item1');
var item1 = s.options[s.selectedIndex].value;

if (item1 = 1) {
    alert("Type1")
}
else if (item1 = 10) {
    alert("Type1")
}
else if (item1 = 11) {
    alert("Type2")
}
}
else if (item1 = 20) {
    alert("Type2")
}
else if (item1 = 21) {
    alert("Type3")
}
}
else if (item1 = 25) {
    alert("Type3")
}
}
</SCRIPT>

Also is there a shorter way to do it right? Thanks!

1

I found out how to do it !!!

<SCRIPT>

function between(x, min, max) {
  return x >= min && x <= max;
}

function message() {

var s = document.getElementById('item1');

if (s.value == 0) {
    alert("Please select an option!")
}
else if (between(s.value, 1, 10)) {
    alert("Type1")
}
else if (between(s.value, 11, 20)) {
    alert("Type2")
}
else if (between(s.value, 21, 25)) {
    alert("Type3")
}
}
</SCRIPT>
0

This will solve the problem.

function message(){

var x = $("#item1").selectedIndex;
var item = $("#item1 option")[x].value;

switch(item){
   case 1...10:
        alert("TYPE1");
        break;
   case 11...20:
        alert("TYPE2");
        break;
   case 21...25:
        alert("TYPE3");
        break;        
    default:
        alert("None Selected");
        console.log(item);
        break;
    }

}
  • This code did not work for me! – Prasanna Mar 16 '16 at 1:57
  • Sorry about that forgot to add the index – jackotonye Mar 17 '16 at 20:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.