• Resolved ragos

    (@ragos)


    Hi, I’ve been trying to solve this for a while and I can’t

    I want to be able to modify the value of a ‘select’ element in a form by using Javascript/jQuery

    The values are already created on the select element, I just want to change to a certain value through a script

    I’m writing this script in a ‘html’ element in Elementor. I’ve tried A LOT of other ways, but I can’t do it.

    jQuery(“#select-2-field”).val(‘user’);

    “select-2-field” is supposed to be the ID of the ‘select’ element of Forminator

    How should I do it?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter ragos

    (@ragos)

    Update: I’m trying this now, which I think it’s supposed to work, but it doesn’t…

    <script>
    
    jQuery(document).ready(function() {
    
        jQuery("#select-2-field").change(function() {
            var val = jQuery(this).val();
            jQuery("#select-3-field").val(val);
        });
    });
    
    </script>

    I just want to update the value of a select element, based on the previous select element.

    • This reply was modified 4 years, 10 months ago by ragos.
    Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @ragos

    I hope you are doing well.

    I am afraid it isn’t possible, the Forminator value created on the select field in the fly won’t be handled with the plugin unless the #select-3-field value does exist while you created the form in the WordPress dashboard, so you would need to select it using JS and also set the test using

    jQuery('#select-3 .select2-selection__rendered').text(val);

    That prints the “label” for the selected element.

    To find more on how to select the option:
    https://stackoverflow.com/a/10911660
    https://stackoverflow.com/questions/4864620/how-to-use-jquery-to-select-a-dropdown-option

    But if this value doesn’t exist in the select field when creating the form it won’t work.

    However, it does work if you use a regular input field so you just set the input value based on Select field value.

    If you have a small number of Select options another alternative is working with conditionals in your Field > conditionals.

    The complete documentation is found at https://premium.wpmudev.org/docs/wpmu-dev-plugins/forminator/

    Let us know if you have any additional question.
    Best Regards
    Patrick Freitas

    Thread Starter ragos

    (@ragos)

    Thanks for the answer.

    You’re saying I can’t use something like an eventHandler to see if the ‘select’ element changes?

    Why can’t I do something like:

    document.getElementById('select-2-field').addEventListener('change', function_name, 
    false);

    I’ve already accomplished changing/selecting an option from the other ‘select’ element, now I want to do that when the first ‘select’ element changes. That’s why I was trying with an eventHandler with it’s ID, but it’s not working

    Thanks again for the answer

    Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @ragos

    You can listen to the event on change without any issue.

    For example this code would work fine:

    jQuery(document).ready(function() {
    
        jQuery("#select-2-field").change(function() {
            console.log('It works');
        });
    });

    But there are some tricks here, it need to load after Forminator in the HTML, so make sure it is loading on the footer, you can apply the code as a mu-plugin file following this guide: https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins

    The code should looks like:

    <?php
    
    add_action( 'wp_footer', function(){
    
      global $post;
    
      if( ! $post instanceof WP_Post || ! has_shortcode( $post->post_content, 'forminator_form' ) ) {
          return;
      }
    
      ?>
      <script type="text/javascript">
    
          jQuery(document).ready(function() {
    
            jQuery("#select-2-field").change(function() {
                console.log('It works');
            });
            
        });
    
      </script>
    
      <?php
    }, 999 );

    But it only works if your form isn’t loading from Ajax:
    https://monosnap.com/file/t39S78PKh6siUCacBTyf2FGxqsYpyA

    In case you load from Ajax there is an event that you can trigger,

    after.load.forminator

    You can find a sample of usage in this link:
    https://gist.github.com/patrickfreitasdev/a6943c0682334a5ec2d653981cbbccdb

    Now, there are the limitations that I let you know on the first reply, you can only select options that do exist on your form when creating it:

    https://monosnap.com/file/XECHZ16X12xt6tHwqtaAy1IrvF7nFI

    It won’t work if you would like to create a new Select option in the fly, but if you add this Select 2 Val inside an input field would work without issue.

    Best Regards
    Patrick Freitas

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘How to give value to ‘select’ element through Javascript?’ is closed to new replies.