Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Setting checkboxes on/off from Javascript

 
dennis zined
Ranch Hand
Posts: 330
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sreenath reddy
Ranch Hand
Posts: 415
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI

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
Posts: 330
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

document.getElementById('myForm').myCheckBox.value

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

hope this helps.
 
dennis zined
Ranch Hand
Posts: 330
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Murphy / Sreenat. I got it working already...thanks a bunch for all your help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic