Reputation: 5402
I have a form where the user can 1.) check one option and be done. or 2.) check the other option and fill out a text field.
Whole thing is, after all is said and done I'd like for my alert to show, but it doesn't.
$('.know-your-role').show('fast', function() {
var $checkboxes = $('input:checkbox.checkchoice');
if($checkboxes.is(':checked')){
// show this after checked and the input has been filled.
alert('cool');
}else if($checkboxes.is(':checked') & $(".year").va() != "" ){
alert('cool');
}
});
How do I get the alert to show after all requirements (checkboxes and input) have been met? I've made a fiddle here to show what I'm working with.
Thank you for your help in advance.
Upvotes: 1
Views: 120
Reputation: 4863
I would suggest having two events, one on the 'Teacher' check box being checked and one on the year field being completed. Both events can trigger a single function that shows your alert and whatever other logic you want. So there is little duplication.
This helps to separate the events from the logic that shows/hides the year field and more easily allows you to perform different actions for the two events if that's a requirement.
Upvotes: 0
Reputation: 50797
As well as the previous correct answers (missing &
and misspelled val
) there is a more fundamental logical issue here. You seem to have this structure:
if (conditionA) {
// behaviorA
} else if (conditionA && conditionB) {
// behaviorB
}
You will never reach behaviorB with such logic. If conditionA
fails then conditionA && conditionB
will certainly also fail.
Do you need to reverse the order of your if
and else-if
blocks?
Upvotes: 1
Reputation: 324650
You should use &&
instead of &
for boolean comparisons, and also you appear to have mis-typed val
as va
.
Upvotes: 0
Reputation: 26940
Missing '&'
$checkboxes.is(':checked') && $(".year").val() != ""
Upvotes: 0