Search code examples
salesforcevisualforce

Get values of form field in javascript from a visualforce page


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


Solution

  • You can just give it an id, then use document.getElementById() to grab it:

    <apex:inputCheckbox id="theCheckBox" value="{!Contact.Some_Checkbox__c}" onClick="alert('checked? ' + document.getElementById('{!$Component.theCheckBox}').checked);"/>
    

    Note that if it's inside other elements with you may need to drill down to it, so if it was inside an <apex:pageBlockSection> with id="theSection" then you'd need:

    document.getElementById('{!$Component.theSection.theCheckBox}');
    

    Whether you need to do this or not depends on where your javascript is in the page with relation to the checkbox. Good luck!