views:

53

answers:

4

I found this answer before, to fire an alert if the button is pressed but the checkbox isn't checked.

Why won't this work?

I have this in the head

<script src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt;

And this in the body:

<input value="1"  type="checkbox" name="salgsvilkar" ID="checkbox2"  style="float:left;"
       onclick="document.getElementById('scrollwrap').style.cssText='border-color:#85c222; background-color:#E5F7C7;';" /><label for="checkbox2" class="akslabel">Salgs og leveringsvilkår er lest og akseptert</label>
        </span>

         {literal}
            <script type="text/javascript">
$(function() {
   //checkbox
   $("#checkbox2").click(function(){
       //if this...
       //alert("this")...
       if($("#checkbox2").is(':checked'))
       {              
          alert("im checked");
       }
   });
   //button
   $("#fullfor_btn").click(function(e){
       if(!$("#checkbox2").is(':checked'))
       {
           alert("you did not check the agree to terms..."); 
           e.preventDefault();
       }
   });
 }
</script>
  {/literal}

This on another .tpl:

<label></label>
        <button type="submit" class="submit" name="{$method}" id="fullfor_btn" title="Fullfør bestillingen nå" value="">&nbsp;</button>

What could be going wrong? The jQuery doesn't fire anything at all.

update the id terms was changed to checkbox2, still nothing happening.

+1  A: 

There isn't any element with id="terms" in your HTML.

Álvaro G. Vicario
Thanks, just changed it and updated the question, but it still won't fire.
Kyle Sevenoaks
+2  A: 

Your checkbox has the id "checkbox2", but you are trying to access it using the id "terms" in the click handler for the button.

update

You are missing the closing parenthesis for the $(function(){...}) call. This throws a syntax error when the page is loaded, and the code in the script block is never run. Change the last line in the script block from:

}

to:

});

I have tested the code with this change, and it works fine.

Guffa
+1  A: 

to fire an alert if the button is pressed but the checkbox isn't checked.

Try this if you press but the check box is not checked as you said above:

 $("#checkbox2").click(function(){
   if($("#checkbox2").is(':checked') == false)
   {              
      alert("im checked");
   }
 });

.

Updated based on comment:

To show an alert if checked, you can simple do like:

 $("#checkbox2").click(function(){
   if($("#checkbox2").is(':checked'))
   {              
      alert("im checked");
   }
 });

Also from your code, it seems that you are missing the ); at the end for the:

$(function()
Sarfraz
Thanks, I'm totally new to jQuery, but also there's some in there to say that if it is checked, show an alert.. But that doesn't happen either.
Kyle Sevenoaks
@Kyle Sevenoaks: see my updated answer please.
Sarfraz
Thanks, that's already in the script..
Kyle Sevenoaks
@Kyle Sevenoaks: you are missing `);` at the end, see my asnwer plz
Sarfraz
Aha! Thanks :) One more thing, can I add more than one function to the selector?
Kyle Sevenoaks
@Kyle Sevenoaks: yes you can.
Sarfraz
:) Great, I'll see if I can add one, will probably be back here in a while to ask what's going wrong with that haha.
Kyle Sevenoaks
+2  A: 

Try this:

$(document).ready(function() {
  $("#checkbox2").click(function() {
    if ($(this).attr("checked")) {
      alert("im checked");
    }
  });

  $("#fullfor_btn").click(function(e) {
    if ($("#checkbox2").attr("checked") == false) {
      alert("you did not check the agree to terms..."); 
      e.preventDefault();
    }
  });
});
GlenCrawford