Forum
Javascript with Forms App
I have a client that we are building a custom order form for. Almost complete except for the auto totaling of of price fields for sub-total and total. We are implementing a JavaScript function to complete the action but it will not work inside of perch:form or perch:input tags.
This Works:
<input type="text" id="value1" class="price"> <input type="text" id="value2" class="price"> <input type="text" disabled="disabled" id="result">
$(document).ready(function(){ $(".price").keyup(function(){ var val1 = +$("#value1").val(); var val2 = +$("#value2").val(); $("#result").val(val1+val2); }); });
This Does't Work:
<perch:form> <perch:input type="text" id="value1" class="price"> <perch:input type="text" id="value2" class="price"> <perch:input type="text" disabled="disabled" id="result"> </perch:form>
$(document).ready(function(){ $(".price").keyup(function(){ var val1 = +$("#value1").val(); var val2 = +$("#value2").val(); $("#result").val(val1+val2); }); });
Trying to understand why it does not work inside of "perch" tags. Thanks.
Hello Jonathan,
The
id
s inside Perch forms are prefixed with a value likeform1_
. Sovalue1
would beform1_value1
.Instead of relying on
id
to get the input fields, try using data attributesdata-
and modify your Javascript accordingly.Correction: It is working within <perch:form> but not with a <perch:input> tag.
Hussein
Can you show an example?
Assuming you have a form like so:
If you only have a single occurrence of the form on the page:
Does this help?
Hussein Al Hammad
Here is my javascript code (i know is wrong) . Also the form is viewable online at https://dev.trishkaufmann.com/orderform
Hussein Al Hammad
I got it working like this:
Instead of using
price
,price2
,price3
, etc. they can all bedata-input="price"
and you can loop over them with jQuery'seach()
:Hussein Al Hammad
Nice! Thanks. Any suggestion on how to get the calculated amount from sub total and add it to the shipping to automatically have the total calculated?