0

I have some issues. I have checkbox in my jsp page. So if the checkbox is checked i have to set a boolean variable to true else false. I tried something like this

<script>
$(document).ready(function() {
    $(".confirmCheckbox").change(function() {
        boolean confirmAssignee = false;
        $(this).attr("checked") {
            confirmAssignee = true;
        }
        alert(confirmAssignee);
    });
});

But it is giving this error

SyntaxError: missing ; before statement
$(this).attr("checked") {

I have mutliple checkboxes on the page. Sample code is like this :

for loop(----)
<input type="checkbox" id="confirmBox-${loopCounter.index}" class = "confirmCheckbox">

Any help will be appreciated. Ask anything if required.

2

5 Answers 5

3

You need to use var while defining variable and use this.checked or .prop() to get whether checkbox is checked or not.

Simple statement will do it.

var confirmAssignee = this.checked; //OR $(this).prop("checked");

Complete Script

$(document).ready(function() {
    $(".confirmCheckbox").change(function() {
        var confirmAssignee = false;

        if (this.checked) {
            confirmAssignee = true;
        }
        alert(confirmAssignee);
    });
});
Sign up to request clarification or add additional context in comments.

Comments

2

There is not boolean type use var

Change

boolean confirmAssignee = false;

To

var confirmAssignee = false;

You also miss the if in the state $(this).attr("checked") { it should be if($(this).attr("checked")) {

Note use prop for checkbox instead of attr as suggest be jQuery doc.

For example, selectedIndex, tagName, nodeName, nodeType, ownerDocument, defaultChecked, and defaultSelected should be retrieved and set with the .prop() method. Prior to jQuery 1.6, these properties were retrievable with the .attr() method, but this was not within the scope of attr. These do not have corresponding attributes and are only properties.

You can simplify code like

Live Demo

$(".confirmCheckbox").change(function () {   
    alert(this.checked);
});

Comments

0

Javascript have auto data types you didn't need to declare data types it is automatically taken in javascript. To check the checkbox using this.checked

 $(".confirmCheckbox").change(function () {
        var confirmAssignee = false;

        if (this.checked) {

            confirmAssignee = true;
        }
        alert(confirmAssignee);
    });

Comments

0

Two problem in your code one there is no boolean datatype in javascript use var instead of boolean

secondly you need to check if checkbox is checked or not in if condition

use below code:

    $(document).ready(function() {

$( ".confirmCheckbox" ).change(function() {

var confirmAssignee = false;
if($(this).attr("checked")) {

}
alert(confirmAssignee);
});
 });

Comments

0

1)use var instead of of boolean

var confirmAssignee = false;

2)use checkbox checked property

if($(this).is(':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.