skip to Main Content

I have a form with steps created with GravitForms/WordPress.

I created a function to copy dropdown value to an input value and a div from the first page to the second page.
At the first time, it works fine, but if if go back from page 2 to page 1 et change the select value, the input value doesn’t update.

Is there any way to force triggering onChange of select dropdown ?

My code :

var select_dates = document.getElementById("input_4_3");
select_dates.addEventListener("change", function() {
    document.getElementById("date_left").innerHTML = select_dates.value;
    document.getElementById("input_4_33").value = select_dates.value;
    console.log(select_dates.value);
});

2

Answers


  1. You tagged jQuery so here is a jQuery answer

    Change the dropdown and then click the link

    You possibly load the selects using AJAX so try this delegation

    ctrl-left arrow to come back

    $(function() {
      $(document).on("change", "#input_4_3", function() {
        $("#date_left").html(this.value)
        $("#input_4_33").val(this.value)
        console.log(this.value);
      }).change();
    })
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <select id="input_4_3">
      <option value="">Please select</option>
      <option value="D 1">Date 1</option>
      <option value="D 2">Date 2</option>
    </select>
    <span id="date_left"></span>
    <input type="text" id="input_4_33" />
    <a href="https://placeholder.com">Go somewhere</a>
    Login or Signup to reply.
  2. You can trigger change event on select element using this:

    $('#input_4_3').trigger('change');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search