0

Problem: Unable to successful detect click or change events on a checkbox.

Intent: The overall intent is to detect a change or click on a checkbox and then use jquery’s show and hide methods to display / hide elements.

My approaches so far include the following:

Method 1: Onclick Handler

HTML

<div class="col-md-1"><center>
    <label><p>Recurring</p>
    <input type="checkbox" onclick="toggleRecur()"></input></label></center>
</div>

JAVASCRIPT

function toggleRecur(){
console.log("something clicked"); }

Method 2: jquery EventListener HTML

<div class="col-md-1"><center>
     <label><p>Recurring</p>
     <input type="checkbox" id="task_recur" ></input></label></center>
  </div>

Javascript/jquery

$(document).on("checked","#task_recur",function(){         
        alert("something checked");
});

Method 3: jquery Listener 2

$('input[id=task_recur]').change(function(){

if($(this).is(':checked'))
    alert("czech it");

});

Unfortunately, none of the above worked, whether inside or out of the document load. I'm currently running under chrome. Any help would be so appreciated!

4
  • You should try the onchange event instead of onclick or with Jquery $('#task_recur').change(function() { alert('clicked'); }); Commented Aug 25, 2015 at 11:34
  • This on works for me, jsfiddle.net/p4sv06sj use document.ready Commented Aug 25, 2015 at 11:35
  • 1
    jsfiddle.net/arunpjohny/gefknLbp/1 - works Commented Aug 25, 2015 at 11:36
  • I generally used the third method. And it should work. Check in your html that have you used the id with which you are selecting the elelment. Commented Aug 25, 2015 at 11:37

3 Answers 3

1

First Option

function toggleRecur(){
alert("something clicked"); }
<div class="col-md-1"><center>
    <label><p>Recurring</p>
    <input type="checkbox" onclick="toggleRecur()"></input></label></center>
</div>

2nd option

document.getElementById('task_recur').onclick = function() {         
        alert("something checked");
   }
<div class="col-md-1"><center>
    <label><p>Recurring</p>
    <input type="checkbox" id="task_recur" /></label></center>
</div>

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

2 Comments

some people just use the downvote button for no reason
@LuthandoLoot I've tried both approaches and neither seems to work for me. Let me be more specific. Option #1 works, but only on page refresh, not on click., which indicates that there must be something elsewhere in the html or javascript that is interfering with something. I'll continue to experiment. +1 for including the snippets above.
0

I can see bug in only,

$(document).on("change", "#task_recur", function () { // event should be change not checked
    alert("delegated event");
});

Also,

if($(this).is(':checked')) can be replaced with simply if(this.checked) {}

DEMO

4 Comments

@kovogel some people do it intentionaly to check their downvoting power
@TributetoAPJKalamSir Thanks for the input. What's strange is that I literally copied and pasted the code, but didn't work. I did observe upon inspection through chrome extension, that some HTML is being generated around my element for some reason. That's the problem with using templates. Not sure if this is a red herring, but the fact that none of the proven solutions seem to be working for me makes me believe that something on the tangent is afoot. Thanks again.
@dellaboemia can you provide compete code...will check if I can help you
@dellaboemia solution should infact wok even when template is doing its job. As we are delegating an event and also our selector is id selector . strange...
0

With pure javascript:

document.getElementById('task_recur').onclick = function() {
    if (this.checked) {
        alert("Checked);
    }
};

With jquery:

$(document).on("change", "#task_recur", function(){         
    alert("something checked");
});


$('#task_recur').change(function() {
    if($(this).is(':checked'))
        alert("czech it");
});

1 Comment

tried both approaches, but no luck. As I mention below, I think I may have some other issue that is interfering with things. I will continue to work the problem and advise should I discover something interesting.

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.