Script in an embedded form

Labels

This Discussion is public

Notifications

Hi,

I've a form in which I placed a script that duplicates the contents of a field in another field. Since this form is embedded in a parent form, I adapted the script and modified the field names (item_meta). For that, I added the section ID I got by inspecting the web page. Wishing to integrate the form with other parent forms, the section ID will change. Is it possible to add code to automatically detect the section ID?

The script is below with [1150] as Section ID:

<script type="text/javascript">
jQuery(document).ready(function($){
var sourceField = $("[name='item_meta[1150][0][1121]']").val();
$("[name='item_meta[1150][0][1130]']").val(sourceField);
$("[name='item_meta[1150][0][1130]']").trigger({ type:'change', originalEvent:'custom' });

$("[name='item_meta[1150][0][1121]']").change(function(){
var sourceField = $("[name='item_meta[1150][0][1121]']").val();
$("[name='item_meta[1150][0][1130]']").val(sourceField); //START field key
$("[name='item_meta[1150][0][1130]']").trigger({ type:'change', originalEvent:'custom' });
});

});
</script>

Thank you.

My solution is:

<script type="text/javascript">
jQuery(document).ready(function($){
"use strict"
var sourceNum = "#" + $("[id^=field_y0p0c3-]").attr('id'); // source field
$(sourceNum).on("change", function() {
var sourceNum = "#" + $("[id^=field_y0p0c3-]").attr('id'); // source again
var sourceField = $(sourceNum).val();  // read source content

var destinationStart = "#" + $("[id^=field_yvkqi3-]").attr('id'); // destination_1 field
$(destinationStart).val(sourceField);  // store data
$(destinationStart).trigger({ type:'change', originalEvent:'custom' });

var destinationEnd = "#" + $("[id^=field_16spg3-]").attr('id'); // destination_2 field
$(destinationEnd).val(sourceField); // store data
$(destinationEnd).trigger({ type:'change', originalEvent:'custom' });
});
});
</script>

Reply to this Discussion