11

I have 2 drop downs in HTML both representing months. So I want a validation like following.

If I select the first drop down month as April, then the next drop-down menu should start from the month April. If the first one is changed to June then the second should change to June.

<div id="head2" style="width:15%;float:right;margin-left:5px;">
    <select id='gMonth2' onchange="show_month()">
    <option value=''>--Select Month--</option>
    <option selected value='1'>January</option>
    <option value='2'>February</option>
    <option value='3'>March</option>
    <option value='4'>April</option>
    <option value='5'>May</option>
    <option value='6'>June</option>
    <option value='7'>July</option>
    <option value='8'>August</option>
    <option value='9'>September</option>
    <option value='10'>October</option>
    <option value='11'>November</option>
    <option value='12'>December</option>
    </select> 
    </div>
    
<div id="head1" style="width:15%;float:right;margin-left:5px;">
        <select id='gMonth1'>
    <option value=''>--Select Month--</option>
    <option selected value='1'>January</option>
    <option value='2'>February</option>
    <option value='3'>March</option>
    <option value='4'>April</option>
    <option value='5'>May</option>
    <option value='6'>June</option>
    <option value='7'>July</option>
    <option value='8'>August</option>
    <option value='9'>September</option>
    <option value='10'>October</option>
    <option value='11'>November</option>
    <option value='12'>December</option>
    </select> 
    </div> 

The function will be:

function show_month()
{
// Comments
}

How should I write that function?

2 Answers 2

5

Easy with jQuery:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"></script>

<script>
$(function(){
  $('#gMonth2').change(function(){
    var month = $(this).val();
    $('#gMonth1').val(month);
  });
 });
 </script>

and skip the onChange event in the first select...

Working example here: http://jsfiddle.net/sCnEZ/1/

Sign up to request clarification or add additional context in comments.

3 Comments

thnkx, but how i will change the attribute to "selected" ?? now i am getting the result but still its showing as january 1st, if i click on dropdown then selected month is highlighted in blue color. but i need the month to come directly. it should not show january everytime.
@user1638279 What do you mean, you are using the selected attribute in your code in the options for january, that will initially select which month is selected from scratch. If you want the function to show CURRENT moth then this is another question than you started with.
@user1638279 in my example I'm using the change() event. You can of course also try with other events such as hover(), blur() etcetera api.jquery.com/category/events
0

Your JS function could be like this:

function show_month(obj) {
    document.getElementById('gMonth1').selectedIndex = obj.selectedIndex;
}

You should change onchange="show_month()" with onchange="show_month(this)"

Check out this jsfiddle

Comments

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.