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.

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"));
});  

Leave a Reply

Your email address will not be published. Required fields are marked *