1

I have been looking at a lot of examples on how to accomplish this but cannot get it to work. I just want to put a simple if statement within my javascript, and when I add the if, it breaks? I am getting test to report true when checked, and false when unchecked, so what am I missing? - Novice user, so if this a convoluted way to do this, I apologize.

NOT WORKING:

<script>
$('input[name="1"]').click(function() {
   var test = $(this).prop('checked'); //{
     if(test == "true")
     {
      var alert_id = $(this).val();
      var key = $('#key').val();
      $.ajax({  
       type: "POST",  
        url: "functions/database_write.php",
       data: "id1="+alert_id+"&key="+key+"&test="+test,
       success: function(resp){  
             $( ".pop-div" ).slideDown(100,"linear");
            setTimeout(function(){
             $( ".pop-div" ).slideUp(100,"linear");
             }, 1000);
             //alert(resp);
        },  
        error: function(e){  
        alert('Error: ' + e);  
      }
    }    
  })
 });
 </script>

But works, but with every click when the function is written like:

<script>
$('input[name="1"]').click(function() {
   var test = $(this).prop('checked'); //{
     //if(test == "true")
     //{
      var alert_id = $(this).val();
      var key = $('#key').val();
      $.ajax({  
       type: "POST",  
        url: "functions/database_write.php",
       data: "id1="+alert_id+"&key="+key+"&test="+test,
       success: function(resp){  
             $( ".pop-div" ).slideDown(100,"linear");
            setTimeout(function(){
             $( ".pop-div" ).slideUp(100,"linear");
             }, 1000);
             //alert(resp);
        },  
        error: function(e){  
        alert('Error: ' + e);  
      }
    //}    
  })
 });
 </script>
1
  • 1
    What do you mean it breaks? What kind of output are you seeing, if any? Commented Sep 5, 2014 at 17:55

4 Answers 4

2

DEMO:

Change if(test == "true") to if(test) or if(test === true) or to if(this.checked)

HTML:

<label for=input>Click Me</label>
<input id=input type=checkbox  name=1 />

SCRIPT:

    $('input[name="1"]').click( function () {
        var test = $(this).prop('checked');
        if(test){
          alert("checkbox checked");
          var alert_id = $(this).val();
          var key = $('#key').val();
      $.ajax({  
       type: "POST",  
        url: "functions/database_write.php",
       data: "id1="+alert_id+"&key="+key+"&test="+test,
       success: function(resp){  
             $( ".pop-div" ).slideDown(100,"linear");
            setTimeout(function(){
             $( ".pop-div" ).slideUp(100,"linear");
             }, 1000);
        },  
        error: function(e){  
        alert('Error: ' + e);  
      }
  });
     }       
 });

NOTE:

If one of the operands is Boolean, the Boolean operand is converted to 1 if it is true and +0 if it is false.

you can add console.log(test); to see the value of test.

What you were doing was comparing true == "true" a boolean with a string

Read Using the Equality Operators

Sign up to request clarification or add additional context in comments.

Comments

1

The key problem is here that you're using true as string which is why it's not working:

So, use it as boolean:

if(test == true) // not "true"

You can also use like below:

if(test) // it says if test is true

OR,

if(test == 1) // 1 for true 0 for false

Comments

0

Try doing:

<script>

    $('input[name="1"]').click(function() {
       //var test = $(this).prop('checked'); //{
         if(jQuery(this).is(":checked"))
         {
          var alert_id = $(this).val();
          var key = $('#key').val();
          $.ajax({  
           type: "POST",  
            url: "functions/database_write.php",
           data: "id1="+alert_id+"&key="+key+"&test="+test,
           success: function(resp){  
                 $( ".pop-div" ).slideDown(100,"linear");
                setTimeout(function(){
                 $( ".pop-div" ).slideUp(100,"linear");
                 }, 1000);
                 //alert(resp);
            },  
            error: function(e){  
            alert('Error: ' + e);  
          }
        }    
      })
     });
     </script>

Comments

0

Try it like this:

<script>
$('input[name="1"]').click(function() {
   var test = $(this).prop('checked'); //{

      console.log(test);// to see whats is in test

     if(test)
     {
      var alert_id = $(this).val();
      var key = $('#key').val();
      $.ajax({  
       type: "POST",  
        url: "functions/database_write.php",
       data: "id1="+alert_id+"&key="+key+"&test="+test,
       success: function(resp){  
             $( ".pop-div" ).slideDown(100,"linear");
            setTimeout(function(){
             $( ".pop-div" ).slideUp(100,"linear");
             }, 1000);
             //alert(resp);
        },  
        error: function(e){  
        alert('Error: ' + e);  
      }
    }    
  })
 });
 </script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.