Ana
Ana

Reputation:

jquery checkbox alert

I've inherited the following checkbox:

<td><div class="text_note"><label for="terms" class="text button_action">
<input type="checkbox" name="terms" id="terms"
onClick="checkCheckBox(this, 'inf_terms', true)">
&nbsp;I understand and agree to the Terms &amp; Conditions.
</label>&nbsp;<span id="inf_terms">&nbsp;</span></div></td>

I need to write a jquery function (a newbie) so that if the checkbox for the terms of condition is not checked an alert message appears telling the user to check the box before proceeding onto the next page. Thx for any help

Upvotes: 2

Views: 8603

Answers (3)

ScottE
ScottE

Reputation: 21630

if($("#terms:not(:checked)")) {

alert("some message");

};

// (sorry, missed a bracket first time)

Upvotes: 0

TStamper
TStamper

Reputation: 30364

I see you have the onClick javascript event handler in your input tag, which will not be needed if you use the click event function jquery offers, unless you have it doing something that you didn't mention. But to have it set up that it will not go to the next page unless the checkbox is checked you would have to know the button that is clicked to go to the next page, lets say the button id is 'button1'

 $(function(){
  $('#button1').click(function(){  
  if($('#terms').is(':checked'));
  {
  }
  else{
     alert("Please check the checkbox saying you agree with terms");
     return false;
   } 
 });
});

Upvotes: 1

RSolberg
RSolberg

Reputation: 26972

This shows how to setup the onClick event for the checkbox. It will fire each time the checkbox is clicked... I've done the same for a submit button idea as id = buttonID...

$(function() {
   //checkbox
   $("#terms").click(function(){
       //if this...
       //alert("this")...
       if($("#terms").is(':checked'))
       {              
          alert("im checked");
       }
   });
   //button
   $("#buttonID").click(function(e){
       if(!$("#terms").is(':checked'))
       {
           alert("you did not check the agree to terms..."); 
           e.preventDefault();
       }
   });
 }

Upvotes: 2

Related Questions