Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts

Tuesday, March 4, 2014

Strut: Multi-select Checkbox in form

When creating a form, using checkbox to capture user's input is common. So how do we check whether the checkbox is checked when the user submit the form?

Javascript:

// Flag to check whether any checkbox is checked.
var checkB = false;

// Get the form object in the page.
var checkboxLength = document.forms[0];

//Loop through the form elements.
for (var i=0; i<checkboxLength.length; i++)
  {
        if(checkboxLength.elements[i].checked == true){
      checkB = true;
        }  
  }

Tuesday, July 23, 2013

Cookies: Add(Javascript) and read(JSP) wildcard cookies

To add cookie with Javascript:

document.cookie = "tracker="+"<%=Str%>"+"; domain=.domain;path=/;";

Point to note:
To read variable from JSP, double quote is needed.

To read cookie from JSP:

        Cookie[] cookies = request.getCookies();
String test=  "";
int c = cookies.length;
        for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++) {
            if (cookies[loopIndex].getName().equals("tracker")) {
                test +=("Cookie says: " + cookies[loopIndex].getValue());
            }
        }