Reputation: 97
I have a form that is generated from a DB query. The fields generated are part of an array name='monitor[]'
and id='monitor[]'
etc.
I am trying to get jquery to tick a checkbox if another one is ticked.
Here is a bit of the form generated from the DB query...
<td><input type='checkbox' id='monitor[$i]' name='monitor[$i]' value='1' /></td>
<td><input type='checkbox' id='priority[$i]' name='priority[$i]' value='1' onclick='selectMon()'/></td>
<td><select name='position[$i]' onchange='selectMon()'>
<option selected='selected' disabled='true'></option>
<option value='support'>Support</option>
<option value='oppose'>Oppose</option>
<option value='watch'>Watch</option>
</select></td>
Note the onclick event.
The jq...
function selectMon() {
if ($('input[name="priority[]"]').prop('checked')) {
$('input[name="monitor[]"]').attr('checked', true);
}
}
I've tried different variations of the jq. I've tried with \ \ before the square braces. I've tried with defining 'i' (array number). I've searched for the past couple hours.
I'm trying to make it so that if priority is checked monitor will also be checked. Also if a position is selected monitor will be checked.
It seems that if it weren't for the array [] (multiple iteration of the fields) this would be simple.
What am I missing?
Upvotes: 0
Views: 37
Reputation: 21638
You need to pass the index in
onclick='selectMon($i)'
function selectMon(index) {
if ($('input[name="priority[' + index + ']"]').prop('checked')) {
$('input[name="monitor[' + index + ']"]').attr('checked', true);
}
}
Upvotes: 1