user1054080
user1054080

Reputation:

Pass variable from .each loop to if statement within function

I'm a novice at Jquery/Javascript programming so please be gentle ;)

The function below checkes if a radiobutton from a specific group is selected. However I can't get the:

var checkifonechecked = true;

to be passed to the if statement. It returns a Uncaught ReferenceError: checkifonechecked is not defined. How can I pass this var?

function stap3() {
$('input[name=ostap2]').each(function() {
    if (this.checked) {
        var checkifonechecked = true;
    }
});
if(checkifonechecked){  
        $('#stap1, #stap2, #stap4, #stap5, #stap6, #stap7').hide();
        $('#stap3').show();
    } else {
        alert('neen'); }
} 

Many thanx for any assistance,

Upvotes: 1

Views: 1995

Answers (5)

RB.
RB.

Reputation: 37192

Your problem is because you are declaring your checkifonechecked variable inside an anonymous function (AKA a lambda). Once the anonymous function has ended, the variable no longer exists, so attempting to use it in the stap3 function will fail with an "Uncaught ReferenceError" as you describe.

You need to declare your variable somewhere it's guaranteed to execute (e.g. the beginning of your stap3 function), like so:

function stap3() {
    // DECLARE your variable outside the anonymous function.
    var checkifonechecked = false;

    $('input[name=ostap2]').each(function()  // This is the anonymous function.
    {
        if (this.checked) {
            // ASSIGN a value to your variable.
            checkifonechecked = true;
        }
    });

    if(checkifonechecked)
    {  
        $('#stap1, #stap2, #stap4, #stap5, #stap6, #stap7').hide();
        $('#stap3').show();
    } 
    else
    {
        alert('neen'); 
    }
} 

Upvotes: 2

t0ne
t0ne

Reputation: 159

The problem is the scope of checkifonechecked, you need to move it to the stap3 function

function stap3() {
    var checkifonechecked = false;
    $('input[name=ostap2]').each(function () {
        if (this.checked) {
            checkifonechecked = true;
        }
    });

    if (checkifonechecked) {
        $('#stap1, #stap2, #stap4, #stap5, #stap6, #stap7').hide();
        $('#stap3').show();
    } else {
        alert('neen');
    }
}

Upvotes: 0

adeneo
adeneo

Reputation: 318212

You're checking a variable that is declared in a different scope. You'll need to declare the variable in a higher scope:

function stap3() {
    var checkifonechecked = false;
    $('input[name=ostap2]').each(function () {
        if (this.checked) checkifonechecked = true;
    });

    if (checkifonechecked) {
        $('#stap1, #stap2, #stap4, #stap5, #stap6, #stap7').hide();
        $('#stap3').show();
    } else {
        alert('neen');
    }
}

Upvotes: 0

user1646111
user1646111

Reputation:

Can you post the HTML part (The section that contains the form), also, if you defined a variable as global, then, do not repeat (var checkifonechecked = true) just use (checkifonechecked = true).

Now, for resource saving, break the loop once the selected checkbox found, because only one of them can be checked you don't need to see other checkboxes if selected one was found.

Upvotes: 0

wirey00
wirey00

Reputation: 33661

You just need to check how many are actually checked. There's really no need for the each statement or the variable

function stap3() {
   if($('input[name=ostap2]:checked').length){  
        $('#stap1, #stap2, #stap4, #stap5, #stap6, #stap7').hide();
        $('#stap3').show();
    } else {
        alert('neen'); 
    }
} 

Upvotes: 3

Related Questions