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

Extracting the value entered into a form field

  • January 30, 2024
  • 4 replies
  • 805 views

Forum|alt.badge.img+1

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+" input[type=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?

 

 

Best answer by TomG

@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)

4 replies

Shashi
Level 8 ●●●●●●●●
Forum|alt.badge.img+34
  • Level 8 ●●●●●●●●
  • 654 replies
  • January 30, 2024

Try below:

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

 

  


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 6084 replies
  • Answer
  • January 30, 2024

@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)

Forum|alt.badge.img+1
  • Author
  • 2 replies
  • January 31, 2024

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!


Forum|alt.badge.img

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