Track user changes before submission

I am building a website where we need to be able to see if a user changes their initial selection before submitting the form.

For example, we have a question ‘are you over 18?’. The user might first select no, then change their answer to yes.

When we review submitted forms, this should trigger a prompt like ‘this is not the first response selected by the user’.

Does anyone have any ideas how we could implement this? Thanks in advance!

Hi, Will,

You would need to write some custom JavaScript to do this. You could create a hidden field in your form, and then write some JavaScript that populates that field when the user fills out another field.

For instance, if your “Are you over 18?” field is a dropdown with an ID of 1, and you have a hidden field called “Number of answers” with an ID of 2 and a default value of 0, you could do something like this:

$( "#input_1_1" ).change( function() {
  var $old_value = $( "#input_1_2" ).val();
  $( "#input_1_2" ).val( $old_value + 1 );

Every time a user changes the answer to “Are you over 18?” the counter in the hidden field would go up.

I have not tested this code at all, so it might not work, but hopefully it’s enough to get you started. If you need help getting it to work, let me know!

1 Like

Thanks! This seems like it will work.

Is there a way I can streamline the function to work for every field in the form, or do I need to write a section of code to look at each possible change event?

You could do something like this:

$( "input" ).change( function() {
  var $id = $( this ).attr( 'id' );
  var $old_value = $( "#input_1_10" ).val();
  $( "#input_1_10" ).val( $old_value + $id + ' value changed, ' );

(again code is completely untested and might not work)

That would fill your hidden field with:
input_1_1 value changed, input_1_2 value changed, input_1_3 value changed, input_1_1 value changed
which might be really annoying to read through.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.