+ Start a Discussion

javascript to check whether checkbox are checked or not


 I am writing a code where if checkbox is checked then mail will send to desired user otherwise it will show an error "Please select checkbox". Can i get javascript for this?

<apex:pageBlockTable value="{!searchResults}" var="accWrap" id="table"">
 <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
<apex:inputCheckbox value="{!accWrap.selected}" id="inputId"/> 
<apex:column colspan="5" value="{!accWrap.acc.Name}" />
<apex:column colspan="5" value="{!accWrap.acc.Email}" />
<apex:column colspan="5" value="{!accWrap.acc.Phone}" />
Nagendra Prasad ChNagendra Prasad Ch

jQuery code is easy for this.
<script src="https://code.jquery.com/jquery-1.11.1.min.js"> </script>
    j$ = jQuery.noConflict();   
     function validateCheckboxes(){       
            var atLeastOneIsChecked = j$('input:checkbox').is(':checked');
            if(!atLeastOneIsChecked) {
                alert('Nothig is checked ');
                return false;
         return true;

Add onClick function in button.
<apex:commandButton id="getValBtn"  value="Test" onclick="return validateCheckboxes();"  />

Instaed of unsing onclick function in button, u can use .click() in jQuer it self as explained in below article.
Hello C Nagendra Prasad, I have changed my code like this. now it is showing alert but after selecting checkbox its not sending mail to user