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

Question

Question

Javascript only look at the visible fields

asked on August 31, 2016

I am still learning Javascript so please bear with me :)

 

We have a form that has these fields:

Student Count: (numerical values)                 CSS: student

One Student: 200                                           CSS: one

Two Students: 250                                         CSS: two

Three Students: 300                                      CSS: three

Four or more: text, no calculation                 CSS: four

Total:                                                              CSS: total

I have already setup Field Rules that hide "Two Students" field if Student Count = 1 (for example) etc.

 

I need a javascript that will look which field is visible and then copy the default value to the Total field.

 

Any help is much appreciated, perhaps I am overthinking the code since I have already tried is many different ways without much success.

 

Thank you!

0 0

Answer

SELECTED ANSWER
replied on September 1, 2016

If you are using Forms that supports formula, you can set the formula on the total field like this:

=IF(student=1,one, IF(student=2, two, IF(student=3, three, four)))

If your Forms does not support formula, try custom script like this:

$(document).ready(function(){
  $(".student input").change(function(){
    var student = $(".student input").val();
    console.log(student);
    if (student == "1") {
      $(".total input").val($(".one input").val());
    }
    else if (student == "2") {
      $(".total input").val($(".two input").val());
    }
  });
})

 

0 0

Replies

replied on August 31, 2016

Would it be easier to change the Student Count field to a drop down field with values 1, 2, 3, 4, more, and then select "Assign values to choices" and give each one the corresponding values of 200, 250, 300, text? You could then use a little javascript to show the selected value in the Total field.

0 0
replied on September 1, 2016

That would work however, I am using a lookup from a database and a lot of times there are 4+ students so I would have to figure out how to code "4 or more" into a dropdown - any ideas?

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

Sign in to reply to this post.