A basic form:
<body>
<form method="post">
<select name="details[1234567890123][strComplaintGroup]" id="details[1234567890123][strComplaintGroup]">
<option value="">Select</option>
<option value="1">One</option>
<option value="2">Two</option>
</select>
<select name="details[1234567890123][strComplaintDetail]" id="details[1234567890123][strComplaintDetail]">
<option value="">Select</option>
<option value="1">One</option>
<option value="2">Two</option>
</select>
</form>
</body>
and the jQuery:
$(document).ready(function(){
$('select').on('change', function(){
var strId = $(this).attr('id');
if( strId.indexOf('strComplaintGroup') > 0 ){
var uniq_id = strId.substring(8,21); // details[<13 char uniq_id>][]
var strDetail = '#details[' + uniq_id + '][strComplaintDetail]';
console.log( strDetail );
$(strDetail).hide();
}
});
});
I am trying to use the change the ComplaintDetail options when the ComplaintGroup is changed. My variable console.log with proper values but the ComplaintDetail Id seems to fail as a jQuery selector.
I have tried $('#' + strDetail)
and placing the #
in the string. No Luck.
The only thing I can guess is the []
are causing the failure. Valid names but not valid id/selector? As you can see I had to use $(this)
instead of the id at the top of the JS. If so, how can I overcome this? The []
are required as this is a subform.
Apparently guessed correctly. Changed the []
to -
for the id's and it works.
Maybe this will help someone else, I spent two days trying to figure it out.