2

I have a set of variables that I use to tell if an question in a survey has been answered, and they are initially set to false: q0=false; q1=false; qX=false;. Then in the body, I have several input type="radio"s with .change(function(){ counter_radio(q#,INPUT'S_ID); }); binded to each. This is the code for function counter_radio():

function counter_radio(q,id){
    if (q===false) {
        q=true;
        count++;
        $("#counter").css("width", count);
        $("#percent").text(count + "%");
    }
    $("span#"+id).append(", "+q);
    $("span#"+id).fadeIn();
}

I expect that the counter to only increment once (the first time a radio from a set is selected). However, the counter is incrementing every time the input type=radio changes. My problem is that, q returns true, but q# remains false. In php, I would make q a reference to q# so when q is set to true, q# is also set to true. Is there such a construct in javascript?

4 Answers 4

3

You could change your bound function to:

.change(function(){ q# = counter_radio(q#,INPUT'S_ID); });

and add a return q; to the end of your counter_radio function.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! it works (but it's not as clean as a simple function call).
If you want 'clean' then you really should refactor the whole thing to not use q1-q9 as your variable names :P
This functional approach is IMO far more elegant then the other suggested global object reference.
@jacob I'd create a 'Quiz' class, which would handle the count and percent information. I would then create a 'Question' class, subclass it to get the multi-choice specific questions, and store those in an array inside a Quiz object.
1

As others have said, simple variables like booleans are passed by value, not by reference in Javascript so there is no way to change that.

If you have multiple variables like this:

var q0=false, q1=false, qX=false;

then, it might be easier to put them in an array:

var qArray = [false, false, false];

Then, because arrays and objects are passed in a way that you can modify the original, you can pass the array and an index to your function like this and you are then able to modify the original data from your function:

function counter_radio(q, index, id){
    if (q[index] === false) {
        q[index] = true;
        count++;
        $("#counter").css("width", count);
        $("#percent").text(count + "%");
    }
    $("span#"+id).append(", " + q[index]);
    $("span#"+id).fadeIn();
}

counter_radio(qArray, 2, id);

Comments

1

There's no native syntax for that, but you can e.g. use objects for that:

function counter_radio(q_,id){
    if (q_.ref===false) {
        q_.ref=true;
        count++;
        $("#counter").css("width", count);
        $("#percent").text(count + "%");
    }
    $("span#"+id).append(", "+q);
    $("span#"+id).fadeIn();
}

1 Comment

oooow yes! except now count doesn't increment :/
0

I would collect the values when the user submits the form:

jQuery('radio[name=...]:checked').val()

If you want to warn the user that not all questions have been answered, use the jQuery validation plugin.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.