You are viewing limited content. For full access, please sign in.

Question

Question

JavaScript to change field value after another field is accessed

asked on January 16, 2019

Can I use JavaScript to change a field's value (clear it out) if another field is clicked into?

#q133 (a drop-down pick-list) is the field I want cleared out when the other field is accessed;

#q24 (the radio button field) is the field I want the change to happen on (as soon as it is clicked into).

 

1 0

Answer

SELECTED ANSWER
replied on January 16, 2019 Show version history

Yes, there's actually several ways, but do you want it to clear the dropdown for any radio button, or just a specific value?

In general, the code you need is as follows

$(document).ready(function(){

  // attach click event handler to your radio button
  $('#q24 input').on('click',function(){

    // clear the value of your drop down
    $('#q133 select').val('').change();
  });

});

The .change() after you set the value is important because it will ensure Forms runs all the necessary processes as if a user changed the value.

If you only want this to happen for one of the radio button options, then add :eq(#) after input and enter the index (the option number minus 1).

For example, to add it only to the 2nd radio option you'd use the following:

$('#q24 input:eq(1)')

If you want it to clear if you just click anywhere near the radio fields, then you can remove the "input" part entirely, but then it would trigger even if you clicked the label or the empty space to the right of the form.

Again, you can do this many different ways, but this is a relatively simple example.

5 0
replied on January 17, 2019 Show version history

This worked perfectly, Jason, thank you so much!  The radio button only has one button, to indicate "I Agree" and is a required field so I didn't need to make any changes at all.  Thanks!

1 0

Replies

You are not allowed to reply in this post.
You are not allowed to follow up in this post.

Sign in to reply to this post.