user3681970
user3681970

Reputation: 1281

boolean variable in jquery

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.

Upvotes: 0

Views: 34860

Answers (5)

Nagaraj S
Nagaraj S

Reputation: 13484

1)use var instead of of boolean

var confirmAssignee = false;

2)use checkbox checked property

if($(this).is(':checked')) 

Upvotes: 0

Adil
Adil

Reputation: 148178

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);
});

Upvotes: 2

Vivek Gupta
Vivek Gupta

Reputation: 1055

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);
});
 });

Upvotes: 0

Sudharsan S
Sudharsan S

Reputation: 15403

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);
    });

Upvotes: 0

Satpal
Satpal

Reputation: 133453

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);
    });
});

Upvotes: 3

Related Questions