function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Arvind010Arvind010 

Checkbox validation

I have a check box in my visual force page.If that check box is checked,an alert message should be displayed.

Code:
<apex:inputCheckbox id="box1" value="{!accepted}" />
 
I tried giving the condition,

Code:
alert(document.getElementById("box1").value);
and
document.formname.box1.checked
 
Above mentioned conditions are not working.I dont know how to give the condition.Please provide me the solution.



Richie DRichie D
Is this what you want?

<apex:inputCheckbox onclick="if(this.checked) alert('boo!');"> </apex:inputCheckbox>

R.
jwetzlerjwetzler
If you want to access the id of a component you need to use $Component.  Please refer to the documentation, and also search the forums.  There have been several posts referencing correct usage of $Component.
Arvind010Arvind010
It is working...Thanks a lot.One more thing,I have a submit button.It will be good if the alert displays when i click the submit button.Here,when i check the checkbox, the alert is getting displayed.How to give the condition?Please provide me the solution.


Message Edited by Arvind010 on 08-11-2008 09:16 PM
Richie DRichie D

the solution could be something like:-

<input type="checkbox" id="chkBox"/>

<button onclick="if (chkBox.checked) alert('checked');" >click me</button>

Not tried this but the logic should work.

 

jwetzlerjwetzler
We should be using Visualforce components instead of html components wherever possible.  It's generally necessary anyway to get the binding into the controller.  So you'd want something more like:

Code:
<apex:inputCheckbox id="box1" value="{!accepted}" />
<apex:commandButton value="Click Here" action="{!DoAction}" onclick="alert('{!$Component.box1}')"/>

 This will alert the id of the checkbox component.  It doesn't do anything useful but you can see now how you can use getElementById to get the checkbox and check whether it has been selected or not.


Message Edited by jwetzler on 08-12-2008 07:49 AM
Arvind010Arvind010
I tried with this...onclick="alert('{!$Component.box1}')".I got the alert saying 
"apexpage:theForm:box1".Then i added this in getElementById().But the alert was
not working.Please tell me what should be added inside the getElementById().
I tried with someother conditions also.I didnt get the result.
Richie DRichie D

do this:-

<apex:inputCheckbox id="box1" />
<apex:commandButton value="Click Here" action="{!DoAction}" onclick="alert(document.getElementById('{!$Component.box1}').checked)"/>

Arvind010Arvind010
It is working.Thank you for your solution.