The moose likes Struts and the fly likes Setting checkboxes on/off from Javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Setting checkboxes on/off from Javascript" Watch "Setting checkboxes on/off from Javascript" New topic

Setting checkboxes on/off from Javascript

dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330
Hi all.

I use a struts form bean to populate data on my JSP page which works perfectly fine. I use a boolean attribute for my checkboxes and I can set on or off based on the boolean value. I'm using the <html:checkbox .... html struts tags for the checkbox controls.

The problem I'm having right now is .... I have a javascript that gets triggered by some event (onclick, etc.) which is to uncheck the already checked checkboxes if some data validation process failed. However, I could not get my javascript to check or uncheck the freaking checkbox.

My javascript goes like this...

Please help.

SCJP 1.4<br />SCWCD 1.4
sreenath reddy
Ranch Hand

Joined: Sep 21, 2003
Posts: 415

If u use html:checkbox property="" untill unless u dont have a nattribute named id for that checkbox tag u cnat access that element in javascript using getElmenetById rather u can access that element via form tag like

document.forms[0].elementName.checked = false;

and the element name above corresponds to the property of the check box tag
dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330
thanks for your reply.

I dont have any problems at all accessing the checkbox element and that is not really my problem. My problem is setting the checkbox to check/uncheck from my javascript. If i use regular html tags, I can set it (with no problems at all) dynamically via my javascript but have not had any luck once I start using Struts tags. I'm not sure what struts or jsp have to do with it...
M Murthy
Ranch Hand

Joined: Nov 20, 2004
Posts: 33

Follow these steps to access the html:checkbox element in the javascript

1) Define a styleId for the html:form tag (say myForm)
2) Define a styleId for the html:checkbox tag (say myCheckBox)

then you can access the value of the checkbox using the below syntax


How it works is, the styleId of a struts tag gets translated to the id attribute of the html tag.

hope this helps.

Sindhu Murthy<br />SCJP 1.4<br />SCBCD
dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330
Thanks Murphy / Sreenat. I got it working already...thanks a bunch for all your help.
I agree. Here's the link:
subject: Setting checkboxes on/off from Javascript
It's not a secret anymore!