tags:

views:

43

answers:

4

What's wrong here? The alert function was working until I added this new function to it.

Is there anything I am doing wrong? It just simply doesn't fire the alert anymore.

<input value="1"  type="checkbox" name="salgsvilkar" id="checkbox2"  style="float:left;"
        />

    {literal}
             <script src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt;
                <script type="text/javascript">
    $(function() {
       //checkbox
       $("#scrollwrap").click(function(){
        $("#scrollwrap").toggleClass('highlight');
        });​
    });

    $(function(){
       //button
       $("#fullfor_btn").click(function(e){
           if(!$("#checkbox2").is(':checked') == false)
           {
               alert("Please accept the terms of sale."); 
               e.preventDefault();
           }
       });
     });
    </script>
      {/literal}


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

you can put all your code in the same $(function(){ code }) in your example, you are setting the document ready twice

 $(function() {
   //checkbox
   $("#scrollwrap").click(function(){
    $("#scrollwrap").toggleClass('highlight');
   });​

   //button
   $("#fullfor_btn").click(function(e){
       if($("#checkbox2").is(':checked'))
       {
           alert("Please accept the terms of sale."); 
           e.preventDefault();
       }
   });
 });
meo
Thanks, I added the double doc ready to see if it'd force the alert, But even after removing it, the alert still doesn't fire.
Kyle Sevenoaks
you code works just fine, i think you have a conflict somewhere else in your code. All i can see is `if(!$("#checkbox2").is(':checked') == false)` could be just: `if($("#checkbox2").is(':checked'))`
meo
@meo: That's a moot point, since jQuery event handlers do not overwrite each other. Both handlers would be attached. Try it.
Nick Retallack
@nick: i know it works it was just an optimization the main problem was the if with the double negation. Why the down vote?
meo
+2  A: 

Instead of:

if(!$("#checkbox2").is(':checked') == false) {

why not just use the much more readable:

if($("#checkbox2").is(':checked')) {

EDIT: If your intention is for the alert to fire when the checkbox is not checked:

if(!$("#checkbox2").is(':checked')) {
karim79
Because the alert has to appear if the box is not checked.
Kyle Sevenoaks
@Kyle - in that case, the problem is with that statement :). Just get rid of `== false`
karim79
Thanks :) So if it has `!` it means `not`?
Kyle Sevenoaks
yes exactly, i had corrected this in the example i posted. `!` is a negation
meo
http://www.w3schools.com/JS/js_comparisons.asp
Jakob Kruse
@Kyle - yes, ! means not, so `!$("#checkbox2").is(':checked')` means *not* checked, thus `!$("#checkbox2").is(':checked') == false` is an awfully long and confusing way to say *is* checked.
karim79
Thanks, I'll get onto reading that, just had to fix these things quick :)
Kyle Sevenoaks
+1  A: 

Your logic is probably not what you intended, as karim79 said.

if(!$("#checkbox2").is(':checked') == false)

is identical in function to

if($("#checkbox2").is(':checked') == true)

so your alert will only trigger when the checkbox is checked.

Jakob Kruse
A: 

Works for me.

Though it seems a little odd that you'd trigger the alert when the checkbox is checked, instead of when it is not.

Nick Retallack