i have requirement where i need to check if apex:inputCheckbox is checked, and if it isn't then raise an alert saying that its not checked.
<apex:inputCheckbox value="{!check}"/>
I am raising the alert from a java script. i am using actionfunction to call a apex method after the alert is raised.
What i need is a way to check if the checkbox is checked or not in javascript
Thanks
Prady
Hi
I hope this code should answer your question.
To get Id of any apex element in html/js you have to use $Component global variable as suggested by salesforce.
Amit Jain
If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.
All Answers
Hi,
You can make the condition inside the JavaScript function which checks that that the checkbox is checked or not. If not will populate the message. Try the below code as reference:
/////////////// VF Page ///////////////////////
<apex:page controller="Checkbox" id="p">
<script>
window.onload=
function check()
{
var v=document.getElementById('p:f:ChkId').checked;
alert('**********' +v);
if(v==false)
{
alert('Please Checked this value');
}
}
</script>
<script>
function check2(v)
{
alert(document.getElementById('p:f:ChkId').checked);
}
</script>
<apex:form id="f">
<apex:inputCheckbox id="ChkId" onclick="check2('this.value');" value="{!check}"/>
</apex:form>
</apex:page>
/////////////////// Controller ////////////////////////////
public class Checkbox
{
public boolean check{get;set;}
public Checkbox ()
{
check=false;
}
}
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
Hi
I hope this code should answer your question.
To get Id of any apex element in html/js you have to use $Component global variable as suggested by salesforce.
Amit Jain
If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.