Reputation: 11926
I am trying to retain a selected value in the dropdown after posting the page.
Description of scenario
These are my dropdown options
<select id="JqueryDDownForAvailableFrom">
<option value="17:00">17:00</option>
<option value="18:00">18:00</option>
</select>
Before submitting the Page, I am assigning the value to a variable using the below line.
var selectedVal=$("#JqueryDDownForAvailableFrom")
After submitting the page the same dropdown should retail its value.
I do not want to change in options, but want to display the selected value after posting the form.
I have used this one:
This is adding anew option to the dropdown.
$('#JqueryDDownForAvailableTo').append('<option selected="true" value='+selectedValTo+'>' +selectedValTo + '</option>')
I have used this one
This is not selecting the selected value:
$('#JqueryDDownForAvailableTo').val(selectedValTo);
I tried to find on google but failed.
Any assistance please.
Upvotes: 2
Views: 13812
Reputation: 294
To retain selected value after submitting form to php
<select id="JqueryDDownForAvailableFrom" name="JqueryDDownForAvailableFrom"><option value="17:00">17:00</option><option value="18:00">18:00</option>
$('#JqueryDDownForAvailableFrom').val(<?php echo $_POST['JqueryDDownForAvailableFrom'];?>)
Upvotes: 0
Reputation: 18588
for getting selected value
var selectedVal = $("#JqueryDDownForAvailableFrom").val();
for setting selected value
$("#JqueryDDownForAvailableFrom").val(selectedVal);
hope this helps you
Upvotes: 2
Reputation: 8333
For single select dom elements, to get the currently selected value:
$('#JqueryDDownForAvailableFrom').val();
To get the currently selected text:
$('#JqueryDDownForAvailableFrom:selected').text();
Upvotes: 0