This solution explains how to mirror user input from a form field to a non-form text field on the same Webflow page in real-time using JavaScript.
• Determine the selectors for both the input element (e.g., an input field with an ID like "source-input") and the target non-form element (e.g., a div or span with an ID like "mirror-text").
• Ensure these IDs or classes are set in your Webflow designer so they can be referenced in your JavaScript.
• Add a script tag at the bottom of your page or in your custom code area in Webflow to ensure the elements load before the script runs.
• Attach an event listener to the input field using document.querySelector or document.getElementById. Use the "input" event to capture real-time changes.
• Inside the event handler, set the text content of the target element to the current input value.
• Sample code (remember to include this without raw HTML code blocks if needed):
var sourceInput = document.getElementById("source-input");
var mirrorText = document.getElementById("mirror-text");
if (sourceInput && mirrorText) {
sourceInput.addEventListener("input", function() {
mirrorText.textContent = sourceInput.value;
});
}
• Explanation: When a user types in the input field, the event listener triggers and updates the non-form text field in real-time with the current value.
• Save your changes both in the Webflow designer and in the custom code section.
• Preview and type into the form field to verify that the text field updates accordingly.
By identifying the elements, setting up a JavaScript event listener, and updating the text content on the "input" event, you can mirror user input in real-time on your Webflow page efficiently.