mercredi 14 mars 2018

Triple nested JS IF statement

I'm having issues with a triple nested IF statement.

I have a form which has a question with two radio buttons, this question is not always visible. I want to capture the selection made, but as default the radio buttons aren't selected and are still visible in the source code but use CSS display:none to hide. Therefore I need to specify that the data for the selection is only sent (console log for testing) IF a selection is made.

The logic should be, if div is visible, check if Q3_1 selected, if true, send Yes, if false, check if Q3_0 selected, if true, send False, if not selected don't send.

I think it'd be better to have the logic as below, but I couldn't get this to work either.

If div visible and Q3_1 selected send Yes else If div visible and Q3_0 selected send No

Many thanks in advance.

$(function () {
    // Handler for .ready() called.
    if ($("[id^=divWantMedical]").is(":visible")) {
    if  (document.querySelector("    [id$='PreexistingConditions.Question3_1']").checked == true){
        console.log("Send Yes");

    } else {
        if  ((document.querySelector("    [id$='PreexistingConditions.Question3_0']").checked == true){
        console.log("Send No");

    } else {
        console.log("Don't send anything");

    }
    }
}
});

Aucun commentaire:

Enregistrer un commentaire