Extracting the value entered into a form field | XM Community
Skip to main content

Hello!  I am hoping for some help in extracting the value of a form field using Javascript.  I need to know the actual values entered into the fields of a form question, so that I can do some math using them.  I’ve tried the following for the first field in the form:

        var apples = jQuery("#"+this.questionId+'.InputText').eq(0).val();
            or               jQuery("#"+this.questionId+" inputttype=text]").eq(0).val();
            or               jQuery("#"+this.questionId+".InputText:first").val();
            or              $("QR~"+this.questionId+"~1"); 

 

And then to see what I got I use this:

       alert(apples);

 

The alert box always shows null with all the variations above.  What am I doing wrong?

 

 

Try below:

var apples = jQuery("#"+this.questionId+" .InputText:eq(0)").val();
alert(apples);

 

  


@DanBausch,

A couple of those should have worked. So, the problem lies elsewhere:

  1. Did you do it inside the addOnPageSubmit() function? If not, that is your problem. 
  2. If you are using Simple layout, the jQuery selector is incorrect (you also have to load jQuery in the survey header)

Thank you both.  TomG is correct that I was using this in the OnLoad function instead of OnPageSubmit.  I wanted to check the entry before submitting, but I guess I can’t!


I also struggled with this question and after trying out many different ways and some help of ChatGPT this solution worked for me:

 // Get the input element for form field number 6 in question Q2
 var inputField = document.querySelector('.QR-QID1215422704-6.InputText');

I found this ID using the Inspector in my Browser when previewing the survey


Leave a Reply