jQuery change the select box value based on anchor clicked

When some one clicks a link I need the select box value to change

<a class="cli" href="0">Jan</a>
<a class="cli" href="1">Feb</a>
<a class="cli" href="2">Mar</a>
<a class="cli" href="3">Apr</a>
<a class="cli" href="4">May</a>
<a class="cli" href="5">Jun</a>
[...]
<a class="cli" href="11">Dec</a>

<select class="ui-datepicker-month">
<option value="0" style="display: none;">Jan</option>
<option value="1" style="display: none;">Feb</option>
[...]
<option value="9" style="display: none;">Oct</option>
<option selected="selected" value="10" style="display: none;">Nov</option>
<option value="11" style="display: none;">Dec</option>
</select>

Here is what I have so far, thought it was gonna be simple just cant wrap my head around it:

$("a.cli").click(function(event){ //when anchor is clicked
            event.preventDefault();
            $(".ui-datepicker-month").val($(this).text());
        });  
    });

I tried this too

$("a.cli").click(function(event){ //when anchor is clicked
        event.preventDefault();
        $(".ui-datepicker-month").val($(this).attr("href"));
    });  
});

Still didn't work.

Answers:

Answer

Since the value values are saved at the href attribute, use:

$("a.cli").click(function(event){ //when anchor is clicked
    event.preventDefault();
    $(".ui-datepicker-month").val($(this).attr("href"));
});  
Answer

Why change the value and not select the option that has the "clicked" value?

<a class="cli" href="0">Jan</a>
<a class="cli" href="1">Feb</a>
<a class="cli" href="2">Mar</a>


<select id="month" name="month" class="ui-datepicker-month">
<option value="0">Jan</option>
<option value="1">Feb</option>
<option value="2">Mar</option>
</select>

Script:

jQuery('a.cli').click(function(event) {
    event.preventDefault();

    jQuery('#month option').attr('selected', false);
    jQuery('#month option[value="' + jQuery(this).attr('href') + '"]').attr('selected', true);
});

Check it out http://jsfiddle.net/jchiotaka/uaLXU/

Answer

Try this:

...
<a onClick="$('select.ui-datepicker-month').val('1');">Feb</a>
...


<select class="ui-datepicker-month">
...
<option value="1">Feb</option>
...
</select>

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.