Webflow sync, pageviews & more.
NEW

How do I mirror user input from a form field to a non-form text field on the same Webflow page in real-time using JavaScript?

TL;DR
  • Assign unique IDs to the input field and the target text element.
  • Add JavaScript that listens for input events and updates the text content of the target element in real-time.

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.

1. Identify Your Elements

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.

2. Set Up the JavaScript Event Listener

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.

3. Example JavaScript Code

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.

4. Publish and Test

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.

Summary

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.

Rate this answer

Other Webflow Questions