Title: how to include &#8216;onchange&#8217; to trigger a javascript function
Last modified: August 27, 2021

---

# how to include ‘onchange’ to trigger a javascript function

 *  Resolved [2ndbeeston](https://wordpress.org/support/users/2ndbeeston/)
 * (@2ndbeeston)
 * [4 years, 9 months ago](https://wordpress.org/support/topic/how-to-include-onchange-to-trigger-a-javascript-function/)
 * Hi
    I am trying to create a form where users can use radio buttons to select 
   which items (horses) they wish to buy for a charrity race night event.
 * I have incorporated some javascript that counts how many buttons on the page 
   have been checked to put the total into a readonly field that will be used for
   charging and Stripe payment.
    What I need to do is have an onchange field to 
   call the function for each radio button set. ALSO, is there a resset/clear button
   in the form builder? I can’t find one.
 * The javascript that is being called is:
 * function CalcHorseTotal(){
    var HorseTotal; var HorsePrice; HorseTotal = 0; HorsePrice
   = 5;
 * var race1= document.getElementsByName(‘race1’);
    for(var i = 0; i < race1.length;
   i++){ if(race1[i].checked){ HorseTotal = HorseTotal+1; } } var race2= document.
   getElementsByName(‘race2’); for(var i = 0; i < race2.length; i++){ if(race2[i].
   checked){ HorseTotal = HorseTotal+1; } } var race3= document.getElementsByName(‘
   race3’); for(var i = 0; i < race3.length; i++){ if(race3[i].checked){ HorseTotal
   = HorseTotal+1; } } var race4= document.getElementsByName(‘race4’); for(var i
   = 0; i < race4.length; i++){ if(race4[i].checked){ HorseTotal = HorseTotal+1;}}
   var race5= document.getElementsByName(‘race5’); for(var i = 0; i < race5.length;
   i++){ if(race5[i].checked){ HorseTotal = HorseTotal+1; } } totalhorses.value 
   = HorseTotal; alert(“TEST HorseTotal = ” + HorseTotal ); //Here you can put code
   that you want to be executed before the form is submitted } }`
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fhow-to-include-onchange-to-trigger-a-javascript-function%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Plugin Author [EDGARROJAS](https://wordpress.org/support/users/edgarrojas/)
 * (@edgarrojas)
 * [4 years, 9 months ago](https://wordpress.org/support/topic/how-to-include-onchange-to-trigger-a-javascript-function/#post-14821352)
 * Sorry i don’t have the code that would do what you want, probably it would be
   easier to assign a value of each checked box and then just sum them using formulashttps://
   sfmanual.rednao.com/documentation/calculatedfields/creating-formulas/
 * Also regarding the clear button, you can add a ‘submit button’, although it is
   called submit button in its settings it can also be configured to clear the form
   without submitting it.
 * Regards!
 *  Thread Starter [2ndbeeston](https://wordpress.org/support/users/2ndbeeston/)
 * (@2ndbeeston)
 * [4 years, 8 months ago](https://wordpress.org/support/topic/how-to-include-onchange-to-trigger-a-javascript-function/#post-14825281)
 * Have tried this as a first stab at the formula for the Stripe value.
    Can’t figure
   out why it returns the error “SyntaxError: identifier starts immediately after
   numeric literal” on validation
 * TotalValue = 0;
    if($$field_race1$$.value >= 0){TotalValue = TotalValue+1;} if(
   $$field_race2$$.value >= 0){TotalValue = TotalValue+1;} if($$field_race3$$.value
   >= 0){TotalValue = TotalValue+1;} if($$field_race4$$.value >= 0){TotalValue =
   TotalValue+1;} if($$field_race5$$.value >= 0){TotalValue = TotalValue+1;} if(
   $$field_race6$$.value >= 0){TotalValue = TotalValue+1;} if($$field_race7$$.value
   >= 0){TotalValue = TotalValue+1;} if($$field_race8$$.value >= 0){TotalValue =
   TotalValue+1;} if (TotalValue > 0){return TotalValue;}
    -  This reply was modified 4 years, 8 months ago by [2ndbeeston](https://wordpress.org/support/users/2ndbeeston/).
 *  Thread Starter [2ndbeeston](https://wordpress.org/support/users/2ndbeeston/)
 * (@2ndbeeston)
 * [4 years, 8 months ago](https://wordpress.org/support/topic/how-to-include-onchange-to-trigger-a-javascript-function/#post-14829619)
 * Hi
    Taking a different tack after looking at soem other forum responses: [https://wordpress.org/support/topic/get-checkbox-value/](https://wordpress.org/support/topic/get-checkbox-value/)
 * Now trying checkboxes, but still get a validation error on first line
    “SyntaxError:
   identifier starts immediately after numeric literal”
 * if($$field_RaceCheck1$$.Ischecked(‘horse 1’)(TotalValue = 1}
    else(TotalValue
   = 0} if($$field_RaceCheck1$$.Ischecked(‘horse 2’)(TotalValue = TotalValue+1} 
   if($$field_RaceCheck1$$.Ischecked(‘horse 3’)(TotalValue = TotalValue+1} if($$
   field_RaceCheck1$$.Ischecked(‘horse 4’)(TotalValue = TotalValue+1} if($$field_RaceCheck1
   $$.Ischecked(‘horse 5’)(TotalValue = TotalValue+1} $$field_TotalValue$$.value
   = TotalValue

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘how to include ‘onchange’ to trigger a javascript function’ is closed
to new replies.

 * ![](https://ps.w.org/smart-forms/assets/icon-256x256.jpg?rev=1226707)
 * [Smart Forms - when you need more than just a contact form](https://wordpress.org/plugins/smart-forms/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/smart-forms/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/smart-forms/)
 * [Active Topics](https://wordpress.org/support/plugin/smart-forms/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/smart-forms/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/smart-forms/reviews/)

 * 3 replies
 * 2 participants
 * Last reply from: [2ndbeeston](https://wordpress.org/support/users/2ndbeeston/)
 * Last activity: [4 years, 8 months ago](https://wordpress.org/support/topic/how-to-include-onchange-to-trigger-a-javascript-function/#post-14829619)
 * Status: resolved