views:

1594

answers:

3

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

+2  A: 

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();
       }
   });
 }
RSolberg
Unfortunately, this would fail with an empty checked attribute.<input type="checkbox" id="terms" checked="" /> would still fire.
ScottE
is that why you are using .not instead of .is? What about .attr("checked")?
RSolberg
Looks like this was edited after I left my comment, so the context was lost. The above looks fine to me now.
ScottE
k... I have used the .attr("checked") in the past, but it seems like the .is and the .not are better... certainly easier to read...
RSolberg
I imagine the pseudo selectors should be used if it exists, so in this case, no need for attr()
ScottE
+1  A: 

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;
   } 
 });
});
TStamper
A: 

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

alert("some message");

};

// (sorry, missed a bracket first time)

ScottE