I have a form that has a section which allows you to dynamically add sections. 2 of my inputs are a select dropdown which auto-populates a read-only text field. The value of the dropdown has 2 values separated by a space. That's the reason for the indexOf()
and trim()
.
The first section ID look like this frm_section_280-0
.
The 2nd section ID looks like this: frm_section_280-1
...and so on
The dropdown and text-field IDs are similar:
Dropdown ID: field_bev5i972c9c1b67-0
Read-only ID: field_project_id-0
The code I have does the first one correctly. However, when I add another section, the 2nd read-only text field doesn't populate correctly.
var values,
dropdownField = "select[id^=field_bev5i972c9c1b67]";
prjIDfield = "input[id^=field_project_id]";
jQuery(document).on('change', dropdownField, function(){
var parent = jQuery(this).closest('.frm_repeat_sec');
var values = jQuery(dropdownField).val();
var projID = values.substring(values.indexOf(" "));
jQuery(prjIDfield).val(projID.trim());
});
How can I make each dynamic section use the respective dropdown field? I thought using something with jQuery's parent()
function, but can't seem to make it work.
I've made a fiddle here: https://jsfiddle.net/vubaj4s3/ NOTE: The add/remove doesn't work as I couldn't get the JS to work correctly