javascript - Save value of text on radiobutton change (jquery) -


let's i've got 8 radio buttons, , when 8th selected, textfield appears in can type something. if typed in textfield , radio button gets checked, want save text empty textfield , when re-check 8th again, textfield fills inputted data.

i've tried following:

referenceradio.change(function() {     othertextvalue = othertext.val();     console.log(othertextvalue);     if($(this).val()=="8") {         othertext.show().prop('required', true);         othertext.val(othertextvalue);     } else {         othertext.val('');         othertext.hide().prop('required', false);     } }); 

if other information required, please ask, new this.

thanks in advance!

update:

managed working following code:

referenceradio.change(function() {     console.log(othertext.val());     if($(this).val()=="8") {         othertext.show().prop('required', true);         othertext.val(othertextvalue);     } else {         if(othertext.val() != '') {             /* retrieve othertext.val when not empty */             othertextvalue = othertext.val();         }         othertext.val('');         othertext.hide().prop('required', false);     } }); 

think through:

  1. a radio button other 8 selected.
  2. now click 8, , happens:

    othertextvalue = othertext.val(); 
  3. now othertextvalue "".

it should work if put in else clause, before empty text field, , should make clause else if, executed if 8 selected last:

referenceradio.change(function() {     if($(this).val() == "8") {         othertext.show().prop('required', true);         othertext.val(othertextvalue);     } else if(lastradiovalue == "8") {         othertextvalue = othertext.val();         othertext.val('');         othertext.hide().prop('required', false);     }     lastradiovalue = $(this).val(); }); 

Comments