Friends, I am new to scripting and need your help in solving an issue. I have multiple checkbox groups in the form and i want to apply a condition on if any checkbox with attr name is checked. I found many answers here with hard coded name attributes but so far non where all checkbox with same name attribute are checked. Here is my poor code .. hope you friends can make it great.
HTML
<div class="form">
<div class="formbox">
<label>Contact Type:</label>
<div class="checkBox">
<input type="checkbox" id="Check1" name="Contact" value="email" class="required checkField">
<label for="Check1">Email</label>
</div>
<div class="checkBox">
<input type="checkbox" id="Check2" name="Contact" value="phone" class="checkField">
<label for="Check2">Phone</label>
</div>
<div class="checkBox">
<input type="checkbox" id="Check3" name="Contact" value="mail" class="checkField">
<label for="Check3">Mail</label>
</div>
</div>
<div class="formbox">
<label>Fruits:</label>
<div class="checkBox">
<input type="checkbox" id="Apple" name="Fruits" value="email" class="required checkField">
<label for="Apple">Apple</label>
</div>
<div class="checkBox">
<input type="checkbox" id="banana" name="Fruits" value="phone" class="checkField">
<label for="banana">banana</label>
</div>
<div class="checkBox">
<input type="checkbox" id="Orange" name="Fruits" value="mail" class="checkField">
<label for="Orange">Orange</label>
</div>
</div>
</div>
JQUERY SCRIPT
//Trying to get the name attribute of this checkbox
var checkGroupName = $(this).attr('name');
//Trying to get all checkbox with same name attribute.
var SelectAllCheckBoxes = $( '.form' ).find( $( '.checkField' ).attr(checkGroupName) );
//Trying each loop to check if any checkbox in that group is checked or non is checked
$('SelectAllCheckBoxes').each(function(i, obj) {
if( $(this).is(":checked").length > 0 ) {
console.log('IS CHECKED');
} else {
console.log('NOT CHECKED');
}
});
Well my code is defiantly wrong and not working. I just wrote here to explain what i wish to do. Friends kindly help me solve this issue. Any help will be highly appreciated. Thank you in advance
Aucun commentaire:
Enregistrer un commentaire