Dynamically set value of fields with JS

Support General Dynamically set value of fields with JS

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #32581
    shmaltzshmaltz
    Participant

    Hi, I have a frontend form that consists of a bunch of fields, and I need to be able to set the value of the field with JS using a chrome add-on.

    I managed to get the regular text/number fields working properly using $('#field_id').val(new_value);, however I am having a hard time setting the value of the Taxonomy Advanced field and the WYSIWYG fields.

    The Taxonomy Advanced field uses the Select2 library, and based on their docs, I should be able to set the field value using $('#field_id').val(value).trigger('change'); (for value i'm using the taxonomy ID) but it is not working for some reason.

    Can you please give me some direction on how to complete this task?

    Thank you!

    #32606
    Long NguyenLong Nguyen
    Moderator

    Hi,

    The code works as well on your site, screen record https://share.getcloudapp.com/qGuR9Bwm

    $('#taxonomy_advanced').val(5).trigger('change');

    But please notice that it helps you to set the value for the available options in the DOM. While the field type select_advanced (select2) supports searching the other terms (options) via AJAX which are not available in the DOM.

    #32614
    shmaltzshmaltz
    Participant

    Thank you. I realized the code does select the correct item (I'm not using ajax), however it doesn't show the selected item in the box. It still shows the placeholder. Only after I click the drop-down does it show that it's selected...

    Also, can you please share a method on how to set the value of the WYSIWYG field?

    Thank you!

    #32650
    Long NguyenLong Nguyen
    Moderator

    Hi,

    For the WYSIWYG field, just use the field ID and function val() to set the value.

    $('#wysiwyg').val('asdf1234');

    #32787
    shmaltzshmaltz
    Participant

    Thank you.

    For some reason these 2 fields are not working properly when trying to populate the fields with a Chrome extension, however the regular text/number fields work properly.

    Not sure why.

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.