Mehar44 Mehar44 - 11 days ago 4
jQuery Question

jQuery : If condition working but how to check result in all the if conditions

My problem is in this part of the code. I need both of these conditions to be checked for different cases.

if(!printnameoptionfucntions.printnameValidation()){
return false;
}
else if(!optionfucntions.engravingValidation()){
return false;
}


It only check the first condition always.
I have also tried like this

if(!printnameoptionfucntions.printnameValidation()){
return false;
}

if(!optionfucntions.engravingValidation()){
return false;
}


This is the code for
#button-add click


$('#button-add').on('click', function() {
//.product-info select
//ajax_price();

if($(".engravingDropDown option:selected").attr('data-engraving') == 'No Engraving') {
$('#myModal').modal('hide');
}
/* change button text depend on options selection */
if($('#myOptions').is(':has(span)') === true){

$('#select-options').text('REVIEW OR EDIT OPTIONS AND ACCESSORIES');


}else{

$('#select-options').text('SELECT AVAILABLE OPTIONS AND ACCESSORIES');
}
<?php if($showCustomizeBook){ ?>
$('#customizeBookCustomPriceText').show();
<?php } ?>
if(!printnameoptionfucntions.printnameValidation()){
return false;
}
else if(!optionfucntions.engravingValidation()){
return false;
}
checkDropdownToSetButtonvalue();
$('.selectMandatoryOption').hide().text('');
});


Update :

I have different products in my website for which different options in a dropdown are available for customers to choose.
One type of products have
engraving fonts
with fonts options Narkisim, cherokee etc. If user does select any font there appears an input field in which user needs to write some text to be printed. If user selects a font let say
narkisim
and does not enter some text and clicks
#button-add
it should not allow it to add without adding some text.
Other type of products have
Name to be printed
with options outside the book, inside the book or no name. If user does select an option there appears an input field in which user needs to write some text to be printed. If user selects an option let say
inside the book
and does not enter some text and clicks
#button-add
it should not allow it to add without adding some text.
I hope that makes sense now?

Update 2 :

for product with print option

console.log(printnameoptionfucntions.printnameValidation())


it returns false

console.log(optionfucntions.engravingValidation())


it returns true

for product with engraving option

console.log(printnameoptionfucntions.printnameValidation())


it returns true

console.log(optionfucntions.engravingValidation())


it returns false

Answer

Your current checks always fail because you are checking for engraving options AND print options for every product when each product will only have one or the other. This means that every product fails one of the checks. For your checks, you need to first check if the product is a print product or a engraving product.

 var isPrintProduct = // do some check to see if is print product
 var isEgravingProduct = // do some check to see if is engraving product
 if(isPrintProduct && !printnameoptionfucntions.printnameValidation()){
        return false;
 }
 else if(isEgravingProduct && !optionfucntions.engravingValidation()){
        return false;
 } 

Original answer

Currently, BOTH of your attempts are effectively the same

In both cases, your code will check if printnameValidation() returns a falsy value,and if so, you are returning false from the event handler and no further code in the handler is called.

If printnameValidation() returns a truthy value, you go on to check engravingValidation(). If that returns a falsy value, you are returning false from the event handler and no further code in the handler is called.

This means thatengravingValidation() will only ever be called if printnameValidation() evaluates as true and checkDropdownToSetButtonvalue(); will only ever be called if both printnameValidation() and engravingValidation() both return true.

Since you are returning false, the else really doesnt do anything.

If that doesnt answer your question, to help you further, you'll need to explain what exactly your logic should do

I have a hunch the real issue might be in printnameValidation(). It seems like printnameValidation() may be always returning false and you dont realize it. That's the only thing I can see that would cause the second check to never be called.

To debug that, simply add some logging like this (I'd bet the first log is always false):

console.log(printnameoptionfucntions.printnameValidation());
console.log(optionfucntions.engravingValidation());
if(!printnameoptionfucntions.printnameValidation()){
    return false;
}
if(!optionfucntions.engravingValidation()){
    return false;
}

As @cFreed mentioned, once you work out the 'false' issue, you should refactor your checks to:

if(!...printnameValidation || !...engravingValidation) return false;

And as @Pevara mentions, functions is misspelled in printnameoptionfucntions and optionfucntions. You really should fix that too :)

Comments