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
}
6
  • 1
    You haven't closed most of your options </option> And why not just use document.getElementById("month").value ...? Also you need to attach an event listener or an attribute event handler Commented May 18, 2016 at 11:46
  • @NewToJS <option> is a tag with optional closing tag. It's better if you close, but it's not invalid. Exactly like <li> or <td> Commented May 18, 2016 at 11:47
  • @MarcosPérezGude agreed but it is very bad practice. Commented May 18, 2016 at 11:48
  • I agree with the element.value suggestion. As simple as alert(document.getElementById("month").value+ " selected"); Commented May 18, 2016 at 11:49
  • @BenYep you're also missing a couple of semi colons at the end of your javascript Commented May 18, 2016 at 11:50

5 Answers 5

2

JSFiddle: https://jsfiddle.net/ebrnh047/

Your html:

<select id="month">
  <option value="Jan">Jan</option>
  <option value="Feb">Feb</option>
</select>

Try:

var month = document.getElementById("month");

month.onchange = function() {
  if (month.value == "Feb") {
    alert("Feb selected");
  }
}
Sign up to request clarification or add additional context in comments.

9 Comments

@BenYap just updated, try it now. You have to but an event handler.
For this to work you will need to place the script tag after the html element otherwise it will cause an error.
is still not working.. how can i paste my whole code to show
@BenYap added jsfiddle link, take a look
@dev-l gotten my jsfiddle?
|
0

This is a way to do it with JavaScript only:

First, your HTML:

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

Then, your script:

var monthSelect = document.getElementById("month");

monthSelect.onchange = function(){
  var thisValue = this.value;
  alert(thisValue); 
};

This is the fiddle: https://jsfiddle.net/16sn90tp/

Comments

0

Make sure you execute this code on document ready event( window.onload )

  var monthSelect = document.getElementById("month")
  monthSelect.onchange = function() {

    var opt = monthSelect.options[monthSelect.selectedIndex]
    if (opt.text == "Feb") {
      alert("Feb selected")
    }
  }
   <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>

Comments

0
    Jan Feb

    function myFunction() {
        var monthSelect = document.getElementById("month").value;
        if (monthSelect == "Feb") {
            alert("Feb selected");
            return false;
        }
    }
<select id="month" name="month" onchange="myFunction()">
  <option value="Jan">Jan</option> 
  <option value="Feb">Feb</option>
</select>

Comments

-1

Hi add at first a class called "month" to all of your option tags and one class "monthList" to the select like for example this:

<select class="monthList">
    <option class="month">Jan</option>
    <option class="month">Feb</option>
    <option class="month">Mar</option>
    ...
</select>

After this you need a little bit of JQuery:

$(".monthList .month").click(function(){
    var selectedMonth = $(this).text();
    var showText = "you selected " + selectedMonth;
    alert(showText);
});

Try this, it should work.

4 Comments

This is not tagged as jquery. So your answer is not correct
I don't downvote, for your information, I only write the comment.
Agreed, jQuery isn't tagged in this question so if you wish to offer a jQuery solution is would be best that you clearly display how to include the jQuery library.
Yes, i forgott to look, my fault :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.