Reputation: 2174
I am able to get the value stored in the session. Now I want to show the cookie value in my select box as selected value.
I tried with the below code, but unfortunately my cookie value is not getting selected.
Please help me to solve my issue.
<script>
$(document).ready(function(){
$('#continue').click(function() {
var singleValues = $("#select_letter").val();
$.cookie("language", singleValues);
})
alert($.cookie('language')); //getting the correct value which are set in cookie
$('#select_letter option[value="'+$.cookie('language')+'"]').attr('selected','selected');
//This is not working,Not setting the cookie value as selected
});
</script>
</head>
<body>
<select id='select_letter'>
<option>Java</option>
<option>C</option>
<option>php</option>
<option>python</option>
<option>c sharp</option>
</select>
<input type="button" id='continue' value="Save as default value"/>
<!-- On click of this link the current page will load -->
</body>
</html>
Upvotes: 0
Views: 1335
Reputation: 545
You can set the selected value using the following code
$('#select_letter').val($.cookie('language')).attr('selected', true);
Upvotes: 1
Reputation: 1838
There should be "value" in each option
<select id='select_letter'>
<option value="Java">Java</option>
<option value="C">C</option>
<option value="php">php</option>
<option value="python">python</option>
<option value="c sharp">c sharp</option>
</select>
And then
$("#select_letter").val($.cookie("language"));
Upvotes: 1