0

I am trying to select this checkbox and test if it is checked

<input class="controls" id="user_regret_avatar" label="Delete My Picture" name="user[regret_avatar]" type="checkbox" value="1">

with this

if $("#user_regret_avatar").attr('checked') {
    alert("yay");
}
else{
alert("aww");
};

but I am getting

Uncaught SyntaxError: Unexpected identifier 

is my selector bad?

also note - I've seen http://api.jquery.com/prop/, I've checked all the checked tests.. still same issue

Now with fiddle - Can't get the test going

2 Answers 2

1

You need to change the if condition as follows :

if ($("#user_regret_avatar").attr('checked')) {
    alert("yay");
} else {
alert("aww");
};

Note the () after if:

Here is a working JSFiddle

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

2 Comments

you need to bind it to document.ready() check the working fiddle in updated answer
yeah you need to bind it to some event for it to be initialized.
1

The following code worked for me:

<html>
<head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
function displayItems()
{
    //$('#table_numbers input:checkbox:checked')
    if ($("#user_regret_avatar").attr('checked')) {
        alert("yay");
    }
    else {
        alert("aww");
    }
}
    </script>
    <title>SEARCH</title>
</head>
<body>
    <form method="post" action="1004mcout.php">
        <input class="controls" id="user_regret_avatar" label="Delete My Picture" name="user[regret_avatar]" type="checkbox" value="1">
        <input type="button" onclick="displayItems();" />
    </form>

</body> 
</html>

I think you forgot to surround the if statement with brackets:

if ($("#user_regret_avatar").attr('checked')) {...

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.