0

I have a couple of different things happening on one page, and I need to have the selections pulled from an elementor filter form to populate a gravity forms field (ideally a hidden one).

I have this code here:

jQuery(document).ready(function($) {
    function updateGravityFormHiddenFields() {
        var selectedValue = $('#wpc-taxonomy-work_location-3945').val();
        $('#input_3_14').val(selectedValue);
    }

    updateGravityFormHiddenFields();

    $('#wpc-taxonomy-work_location-3945').change(function() {
        updateGravityFormHiddenFields();
    });
});

Which doesn't work annoyingly. I can get it to change/populate once.. But then not again, if someone changes their selection. I'm essentially looking for something like this code, as this doesn't seem to allow for multiple different selections and different fields.

Any ideas/help working this out would be great :)

2 Answers 2

0

$select.find('option:selected').text() should get the name of the selection:

jQuery(document).ready(function($) {
    function updateGravityFormHiddenFields() {
        var $select = $('#wpc-taxonomy-work_location-3945');
        var selectedText = $select.find('option:selected').text(); // Get the selected option's text
        console.log("Selected Name:", selectedText); // Debugging line
        $('#input_3_14').val(selectedText).trigger('change'); // Ensure value update
    }

    // Initial population
    updateGravityFormHiddenFields();

    // Use .on() instead of .change()
    $(document).on('change', '#wpc-taxonomy-work_location-3945', function() {
        console.log("Change detected"); // Debugging line
        updateGravityFormHiddenFields();
    });
});
1
  • Absolute legend! Ta muchly. Commented Mar 13 at 9:20
0

Try this: $(document).on('change', selector, callback) This ensures the event listener remains attached even if the element is dynamically updated.

jQuery(document).ready(function($) {
    function updateGravityFormHiddenFields() {
        var selectedValue = $('#wpc-taxonomy-work_location-3945').val();
        console.log("Selected Value:", selectedValue); // Debugging line
        $('#input_3_14').val(selectedValue).trigger('change'); // Ensure value update
    }

    // Initial population
    updateGravityFormHiddenFields();

    // Use .on() instead of .change()
    $(document).on('change', '#wpc-taxonomy-work_location-3945', function() {
        console.log("Change detected"); // Debugging line
        updateGravityFormHiddenFields();
    });
});
1
  • This is almost perfect.. Just need it to pull the name of the selection instead of value number. Commented Mar 12 at 13:01

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.