Unverified

Name

Calculated-Checkbox-PayPal

Language

HTML

Rating

Voted: 0 by 0 user(s)

Codevault

WebSquadron

Scroll down to see more snippets from this codevault.

Wordpress Compatability

The author has indicated that this snippet is compatable up to wordpress version: 6.4

Code Snippet Plugin Sync

Free & Pro

Download this snippet by clicking the download button, then head over to the Code Snippet Plugin settings in your wordpress admin dashboard, select the import menu then upload this file to import into your wordpress site.

Pro Only (Coming Soon)

You will be able to click a button and sync this snippet to your wordpress site automatically and from your dashboard manage all code snippets across all your wordpress sites that have the Code Snippets Pro plugin installed.

History

Last modified:

22/08/2024

Important Note

This snippet has the following status:

Unverified

This snippet has not been verified, use with caution and at your own risk. See details provided by author in sidebar and click below to find out more.

Calculated-Checkbox-PayPal

 
                    
1<script>
2document.addEventListener('DOMContentLoaded', function() {
3 const optionsField = document.querySelectorAll('input[name="form_fields[options][]"]');
4 const totalField = document.getElementById('form-field-total');
5 const paypalAmountInput = document.getElementById('paypal-amount');
6 const paypalItemNameInput = document.getElementById('paypal-item-name');
7 
8 // CHANGE: Added this line to select the form element
9 const form = document.getElementById('paypal-form');
10 
11 function getOptionPrice(option) {
12 switch(option) {
13 case 'Option 1 - $1':
14 return 1;
15 case 'Option 2 - $1':
16 return 1;
17 case 'Option 3 - $15':
18 return 15;
19 case 'Option 4 - $20':
20 return 20;
21 case 'Option 5 - $25':
22 return 25;
23 default:
24 return 0;
25 }
26 }
27 
28 function calculateTotalAndItemName() {
29 let total = 0;
30 let selectedOptions = [];
31 optionsField.forEach(function(option) {
32 if (option.checked) {
33 total += getOptionPrice(option.value);
34 selectedOptions.push(option.value.split(' - ')[0]); // Extract option name without price
35 }
36 });
37 totalField.value = `$${total.toFixed(2)}`;
38 paypalAmountInput.value = total.toFixed(2);
39 paypalItemNameInput.value = selectedOptions.join(', ');
40 }
41 
42 // Add event listeners to checkboxes
43 optionsField.forEach(function(option) {
44 option.addEventListener('change', calculateTotalAndItemName);
45 });
46 
47 // Initial total and item name calculation
48 calculateTotalAndItemName();
49 
50 // CHANGE: Added this block to prevent the form from submitting when the Enter key is pressed
51 form.addEventListener('keypress', function(event) {
52 if (event.key === 'Enter') {
53 event.preventDefault(); // Prevent the form from submitting
54 }
55 });
56 
57});
58</script>

0

Related Snippets

Please see some snippets below related to this snippet..

Elementor

Unverified

0

Croccoblocks Megamenu overflow issue for full screen on mobile/tablet

Added: 7 months ago

Last Updated: 7 months ago

This simple bit of CSS Code solves the frustrating issue of overflow (sideways scrolling) for mobile devices on the Croccoblocks mega menu when it is set to full-screen.

Elementor

Unverified

5

Elementor Gradients Headings

Added: 2 years ago

Last Updated: 3 months ago

CSS Code for background gradients on headings in Elementor

Elementor

Unverified

1

Colour-Transition

Added: 1 month ago

Last Updated: 1 month ago

Add some GSAP Colour Transition Effect to Heading

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

Performance

AI Verified

35

Remove Unused Javascript

Added: 2 years ago

Last Updated: 1 week ago

Remove Unused Javascript - and improve your Page Speed Insight Score

WooCommerce

Pro Verified

10

Deactivate some WooCommerce Checkout Fields

Added: 2 years ago

Last Updated: 2 months ago

Deactivate some WooCommerce Checkout Fields from showing

Elementor

AI Verified

6

CSS Grid Aid

Added: 9 months ago

Last Updated: 3 months ago

This can be used for any WordPress Builder to aid working with CSS Grids.