user4161733
user4161733

Reputation:

How to access the check box values in JSP

I am very new to JSP and I have the following issue:

I have to get the values of checkboxes that are checked and make use of these values in Java

In my approve.jsp I have a function called validateApprove() which is called when a button is clicked.

function validateApprove() {
    var cc= document.getElementsByName('imagecheckbox');
    var j=0;
    var values = new array();

    for (var i = 0; i < cc.length; i++) { 
        if(cc[i].checked == true) {
            values[j]== cc[i].value;
            j++;
        }
    }

    if (j==0) {
        alert("please check atleast one item");
    } else {
        alert("Are you sure? Do you want to approve " + j + " item(s)");
    }
}

the error I get is: ReferenceError: array is not defined

Why is that error thrown? Can I use array in Java like the following way:

String vals[] = request.getParameterValues("values");

Upvotes: 0

Views: 117

Answers (2)

Manjunathan M
Manjunathan M

Reputation: 25

function validateApprove() {
    var cc=[];
    var temp =  document.getElementsByName('imagecheckbox');
        cc.push(temp);
    var j=0;
    var values = new Array();

    for (var i = 0; i < cc.length; i++) { 
        if(cc[i].checked == true) {
            values[j]== cc[i].value;
            j++;
        }
    }

    if (j==0) {
        alert("please check atleast one item");
    } else {
        alert("Are you sure? Do you want to approve " + j + " item(s)");
    }
}

enter code here

Upvotes: 0

Aniket Kulkarni
Aniket Kulkarni

Reputation: 12993

Mistake in

var values = new array();

Should be

var values = new Array();

A not a in array declaration.

See also

Upvotes: 1

Related Questions