0

i got a form with a select form elements for month.

<select id="month" name="month">
<option value="Jan">Jan</option>
<option value="Feb">Feb</option>
<option>Mar
<option>Apr
<option>May
<option>Jun
<option>Jul
<option>Aug
<option>Sept
<option>Oct
<option>Nov
<option>Dec
</select>

How do i use javascript to get compare the selected value. for example if i select Feb, the javascript will pop up "you selected feb"

var monthSelect = document.getElementById("month")
var opt = monthSelect.options[monthSelect.selectedIndex]
if(opt.text == "Feb")
{
    alert("Feb selected")
    return false
}
Ben Yap
  • 199
  • 1
  • 4
  • 11
  • 1
    You haven't closed most of your options `` And why not just use `document.getElementById("month").value` ...? Also you need to attach an event listener or an attribute event handler – NewToJS May 18 '16 at 11:46
  • @NewToJS `
  • ` or ``
  • – Marcos Pérez Gude May 18 '16 at 11:47
  • @MarcosPérezGude agreed but it is very bad practice. – NewToJS May 18 '16 at 11:48
  • I agree with the `element.value` suggestion. As simple as `alert(document.getElementById("month").value+ " selected");` – Marcos Pérez Gude May 18 '16 at 11:49
  • @BenYep you're also missing a couple of semi colons at the end of your javascript – Rachel Gallen May 18 '16 at 11:50
  • @NewToJS hi, i have close all my options and use `document.getElementById("month").value` but its still not working – Ben Yap May 18 '16 at 11:54