Custom Fields
Re-Envisioned
Support General Dynamically set value of fields with JS

  • Creator
    Topic
  • #32581
    shmaltz
    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!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Author
    Replies
  • #32606
    Long 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
    shmaltz
    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 Nguyen
    Moderator

    Hi,

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

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

    #32787
    shmaltz
    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 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.