1

I have a select field as follows:

<select id="shippingSelect" tabindex="3" onchange="Cart.update();">
    <option value="">Select Shipping Option</option>
    <option value="Free">Free Shipping</option>
    <option value="Reg-Airmail">Registered Airmail</option>
    <option value="EMS-Express">EMS-Express Shipping</option>
    <option value="DHL-Express">DHL-Express Shipping</option>
</select>

I am setting the selected value in html 5 local storage with key "shippingSelect" .

I want to disable the shipping select and display the user last selected option as selected field.

I am able to disable the select field if local storage set.

if (shipEmpty && isEmpty) {
    $("#shippingSelect").attr('disabled', 'disabled');
    $("#shippingSelect").css('pointer-events', 'none');
    $("#shippingSelect").prop("disabled", true);
}

But the select display displays "Select Shipping Options" rather than the last user selected option value in drop down.

I tried using

if (shipEmpty && isEmpty){
$("#shippingSelect option:selected").text() );

How can i display last selected option as Selected Option. The disabled select field must display last selected option.

How is that possible in jQuery ??

update: $("#shippingSelect").val(myObject); was the solution

user3790186
  • 239
  • 6
  • 21
  • 1
    You cannot change the value of a disabled element. You need to set the value, then disable it. – Diodeus - James MacFarlane Jul 17 '14 at 15:49
  • @Diodeus: [really?](http://jsfiddle.net/pLsX3/) Am I missing, or misunderstanding, something in your statement? – David Thomas Jul 17 '14 at 15:53
  • @Diodeus .It is not already disabled. What i meant was that once a user selects a shipping option and adds a product to cart, local storage is set and the option value must reflect the changed value before disabling it...While reloading page it is displaying the first option.."Select Shipping Options" – user3790186 Jul 17 '14 at 15:54
  • @David Thomas The disabled select field must display last selected option. – user3790186 Jul 17 '14 at 15:58
  • @user: yes. I was addressing Diodeus' comment, not your question. – David Thomas Jul 17 '14 at 15:59
  • I tried using if (shipEmpty && isEmpty){ $("#shippingSelect option:selected").text() ); – user3790186 Jul 17 '14 at 16:09
  • `$("#shippingSelect option:selected").text()` simply gets the text of the selected option. I think you want to set the select to **readonly** and set the value with http://stackoverflow.com/q/499405/218196 – Felix Kling Jul 17 '14 at 16:12
  • @Felix Kling Thank you...That did it... $("#shippingSelect").val(myObject); – user3790186 Jul 17 '14 at 16:53

0 Answers0