Note that there are some explanatory texts on larger screens.

plurals
  1. POJquery Checkbox Change
    primarykey
    data
    text
    <p>I have looked through a number of answers on here and none have really helped me. I have noticed a strange issue with my jQuery. I realise that the code is a bit quick and nasty but i will tidy it once i have it working properly. </p> <pre><code>jQuery('input#use_points').change(function() { if(jQuery(this).not(':checked')) { alert('not checked'); jQuery( "#amount" ).val(0); jQuery( "#pounds" ).val(0); jQuery(".ui-slider-handle").css({'left': '0'}); jQuery(".points-text").hide(); jQuery(".use-points-labels").hide(); jQuery("#slider-range-max").hide(); jQuery('#updatecart').click(); } if(jQuery(this).is(':checked')) { alert('is checked'); jQuery( "#amount" ).val(1); jQuery( "#pounds" ).val(0.05); jQuery(".points-text").show(); jQuery(".use-points-labels").show(); jQuery("#slider-range-max").show(); jQuery('#updatecart').click(); } }); </code></pre> <p>You will notice that i have two alerts 'not checked' and 'is checked' The first if statement works as expected. However, if it's not checked and then i click the check box it runs through both statements. Giving me the alert 'not checked' and then followed straight after by 'is checked' </p> <p>However, i don't understand why this is happening since it's only getting called on the change not the click action of the check box. Does anyone have any thoughts ? </p> <p>Thanks in advance.</p> <p>/////////////////////////////// EDIT //////////////////////////////////////////// I have now answered my own question. For some reason writing it this way works:</p> <pre><code>jQuery('input#use_points').change(function() { if(this.checked) { alert('is checked'); jQuery( "#amount" ).val(1); jQuery( "#pounds" ).val(0.05); jQuery(".points-text").show(); jQuery(".use-points-labels").show(); jQuery("#slider-range-max").show(); jQuery('#updatecart').click(); } else { alert('not checked'); jQuery( "#amount" ).val(0); jQuery( "#pounds" ).val(0); jQuery(".ui-slider-handle").css({'left': '0'}); jQuery(".points-text").hide(); jQuery(".use-points-labels").hide(); jQuery("#slider-range-max").hide(); jQuery('#updatecart').click(); } </code></pre> <p>Can anyone explain why ? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload