1

i have the following, which is supposed to write the value of 6 at the cookie NotStarted if the Checkbox NotStarted is checked and the value -1 if the Checkbox is unchecked

$("#NotStarted").change(function () 
{ $.cookie("NotStarted", if($("#NotStarted").is(':checked') == "true") {6} else {-1} , { expires: 20 * 365 }); return false; });

Well it is supposed to do so, since i have syntax errors... What i am doing wrong here?

3 Answers 3

4

You need to use the ternary operator

$("#NotStarted").change(function () {
    $.cookie("NotStarted", $("#NotStarted").is(':checked') ? 6 : -1, {
        expires: 20 * 365
    });
    return false;
});

looks like this can be simplified as(because you can use the checked property of the checkbox NotStarted )

$("#NotStarted").change(function () {
    $.cookie("NotStarted", this.checked ? 6 : -1, {
        expires: 20 * 365
    });
    return false;
});
Sign up to request clarification or add additional context in comments.

Comments

1

Replace

if($("#NotStarted").is(':checked') == "true")

With

if($("#NotStarted").is(':checked'))

Comments

0

Try This:

$("#NotStarted").change(function ()

    { $.cookie("NotStarted", $("#NotStarted").is(':checked') ? 6:-1 , { expires: 20 * 365 });

    return false; });

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.