1

In Webform 8 I am trying to figure out how to populate a text field with a value based on what is chosen on a drop down.

For example: Select Value are states If user selects Deleware then the value for the text field Ratified is equal to 1.

How can I achieve this in Webform 8x?

In Webform 7x I am able to do this with the Conditionals tab but can't seem to be able to figure out how to do this in Webform 8.

1
  • Populating a text field using conditions is not supported in 8.x because 8.x uses Drupal's #states API.
    – jrockowitz
    May 14, 2019 at 0:20

1 Answer 1

2

You have to use JS, go to settings - CSS/JS tab, write code in CUSTOM JAVASCRIPT textarea.

Some code for reference. I got a text input (id is edit-name), a select (id is edit-gender), the id you find enter 'F12' to see. ID is automatically generated by Webform. When I select male, the text input will appear male.

(function ($, Drupal) {'use strict';$(document).ready(function(){
  'use strict';
  var name = $("#edit-name");
  var gender = $("#edit-gender");
  var duty = $("#edit-duty-select"); 
  function ab() {if (gender.val()==='male'){name.val("male");} else {name.val("girl")}};  
  gender.change(ab);  // add a Listener to select
});  
})(jQuery, Drupal);
2
  • This solution worked for me. Thanks!
    – hockey2112
    Mar 1, 2021 at 18:56
  • This is helpful, thanks. Whilst on the face of it the ability you add some JS via the UI in this way is neat, I can't find any documentation that sets out how to make best use of it.
    – Graham
    May 20, 2022 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.