The Community forums are being phased out in favor of a new Slack group.
Add your email address below to get an invitation to join the community slack group

Slack Signup
Newsletter Optin
Help Desk

Dynamic JQuery with Form Embed

Labels

This Discussion is public

Notifications

Took me a bit to figure this out so I thought I'd share it.

I have a section on my form that's a dropdown. The selection made by the user automagically populates another dropdown fields value. Thanks to Jaime for helping me get that sorted out.
Here's an example of the code on the original form

<script type="text/javascript">
jQuery(document).ready(function($){
$(document).on('change', "select[name='item_meta[6789]']",
"select[name='item_meta[6796]']", function(){
var val1 = $("select[name='item_meta[6789]']").val();
var val2 = $("select[name='item_meta[6796]']").val();
$("select[name='item_meta[6796]']").val(val1);
});
});
</script>

However when I embedded the form into another form the above solution wouldn't function. After trial and error and inspect element I found that the Section ID, Field ID and a seperator are assigned to each field. Using the values and seperator from there - voila 🙂 here's the same piece with embed functionality ( note [Section_ID][0][Field_ID] ):

<script type="text/javascript">
jQuery(document).ready(function($){
$(document).on('change', "select[name='item_meta[6834][0][6789]']",
"select[name='item_meta[6834][0][6796]']", function(){
var val1 = $("select[name='item_meta[6834][0][6789]']").val();
var val2 = $("select[name='item_meta[6834][0][6796]']").val();
$("select[name='item_meta[6834][0][6796]']").val(val1);
});
});
</script>

Thank you!!!

@queberican351 Since it's such a solid example, I'll be posting it as a usage sample in the docs. Thanks for posting this publicly for others to use!

That's awesome Skyler. Thank you for your feedback. I should have included that the easiest way to determine the fields would be Google Chrome's "Inspect Element" by right clicking on the field and choosing "inspect" from the options dropdown. Alternatively FireBug for FireFox is another powerTool for finding elements, ID's and other useful information for custom coding.

Hi,

I encounter the problem but with text fields and I can not find the solution.
The script copies the contents of a text field to another text field.
It works well but if the form is embedded in a parent form, the script doesn't work anymore.

How to modify the script so that it can work when the form is integrated with another one?

Discussion closed.