Buy Now
Sign in

JS to fill form field with value

homeopat , April 27 at 10:27
H
homeopat
Hello to everyone,
I have a form where I need to SUM two form fields to the third field - automatically after filling the fields before SUBMIT.
I am trying with this code :
<script>​
​$('.price').keyup(function () {​
​ var sum = 0;​
​ $('.price').each(function() {​
​ sum += Number($(this).val());​
​ });​
​ $('#nezalistovane_0_cena_celkem').val(sum);​
​});​
​</script>
form fields has 'price' in class and resulting field has id 'nezalistovane_0_cena_celkem'
but it doesnt works.
could you help me?
thx
GreyHead
Hi homeopat,
I suggest that you use your browser web developr tools to debug your script. You can also add some debug lines to the code to see exactly what is happening.
Bob
ChronoForms technical support
If you'd like to buy me a coffee or two, thank you very much
H
healyhatman
Try replacing "$(" with "jQuery("
Web developer at SkySpider.com.au
H
homeopat
Bob: what debug liners do you mean? I am using standart Debug on
Healyhatma: didnt help
I did try this
jQuery(document).ready(function() {​
​ jQuery('.form-control A').keyup(function () {​
​ ​
​ var sum = 0;​
​ ​
​ jQuery('.form-control A').each(function() {​
​ sum *= Number(jQuery(this).val());​
​ });​
​ ​
​ jQuery('#nezalistovane_0_cena_celkem').val(sum);​
​ ​
​});​
​});
but still not working.
I forgot said that these fields are in multiplier.
thx
H
healyhatman
Where have you put this code? Have you tried custom javascript element in Designer, with document ready switched on? (And then obviously take out the jQuery(document).ready you have in there
Also and much more importantly: ('.form-control A') probably won't actually select anything, except maybe an "A" element (<a> ?)​
​ inside an object with class form-control. Need to fix your selector.
Web developer at SkySpider.com.au
H
homeopat
I put this code to setup section - on load js script. I am not good in JS, so no I didnt try.
form-control A is name of the class of form fields . you have right, that the selector is not fixed, because each element has class form-control A. So I add to the mentioned field prefix price to the class.
H
healyhatman
Names of things can't have spaces in them
Web developer at SkySpider.com.au
H
homeopat
but form-control A is made by chronoengine automatically.
H
healyhatman
You'll have to trust me on this buddy - you can't have spaces in a name. You've probably put a space in when you've named the items.
Web developer at SkySpider.com.au
H
homeopat
form-control A is not fill in class item (Builder). I have it empty and then I see form-control A in code in this item. when I add price to the class item in builder, then I see price form-control A in code of page.
Maybe we dont understand to each other.
H
healyhatman
Post pictures - of how you have the field set up in Designer, and of how the HTML code looks.
Web developer at SkySpider.com.au
H
homeopat
Here is example of 1 field in builder
ScreenShot1757.jpg
ScreenShot1758.jpg
ScreenShot1759.jpg
<input name="nezalistovane[0][ks]" id="nezalistovane_0_ks" value="" placeholder="" maxlength="11" size="3" class="price form-control A" title="" style="" data-inputmask="" data-load-state="" data-tooltip="" type="text">
Attachments
ScreenShot1757.jpg
ScreenShot1757.jpg
(25.41 KiB)
30 Downloads/Views
ScreenShot1758.jpg
ScreenShot1758.jpg
(18.58 KiB)
32 Downloads/Views
ScreenShot1759.jpg
ScreenShot1759.jpg
(14.78 KiB)
31 Downloads/Views
H
healyhatman
OK I see now - It's two classes.
jQuery('.form-control.A') 
Web developer at SkySpider.com.au
H
homeopat
3 classes when I set price in builder of item .... i have other fields in form, what I dont need to count.