1

I'm building a booking form using WS Form in Wordpress. Because of the several calculations and conditions accuring when selecting the number of people partecipating in the booking, the form is crazy slow and I've been suggested to use Javascript.

Basically for a numeric field labeled "Adults" and a hypothetical value of 3, I have a separate div that says "Adults number: 3", with the number adjusting according to the field value.

Here is what I tried without success

document.addEventListener('DOMContentLoaded', function() {
// Get the text field by its ID
var textField = document.getElementById('wsf-1-field-13');

// Listen for input event on the text field
textField.addEventListener('input', function() {
    // Get the current value of the text field
    var currentValue = textField.value;

    // Update the HTML editor's content
    document.getElementById('adult-number-display').textContent = currentValue;
});
});

where wsf-1-field-13 is the field ID. In the div I used

"Adults number <span id="adult-number-display">1</span>"

However the field is never updating. What am I missing here?

2
  • Can you use jQuery? that will be more easy in jQuery. Commented Feb 20, 2024 at 4:12
  • Well yes. Can you point me in some direction (tutorial on the web, etc...) so that I can learn and try? Commented Feb 20, 2024 at 9:24

1 Answer 1

2

This approach should work,

Modify it for your use case.

//JQuery Code
jQuery( document ).on( 'change', '.from-this', function( e ) {
    e.preventDefault();
    jQuery( '.change-this' ).text( this.value );
} )

HTML Code:

<input type="search" class="from-this" />
<div class="change-this"></div>
1
  • That's super. Thanks a lot Syed! Commented Feb 20, 2024 at 10:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.