Subpar Web Dev
Subpar Web Dev

Reputation: 3260

Why isn't my checkbox event listener listening correctly?

Yes, I've read the related posts on Stack Overflow. Still can't figure out why this isn't working.

HTML:

<input type="checkbox" id="#finished-checker" value="value"> 
<label for="finished-check">Check if assessment is complete.</label>

JavaScript:

$('#finished-checker').change(function(){
    console.log("this.checked = " + this.checked);//TEST
    $.post({
        method: 'POST',
        url: '/Answers/UpdateFinishedValue',
        data: { finished: this.checked },
        success: function (retobj) {
            console.log(retobj);//TEST
        },
        error: function () {
            console.log("Error ...");
        }
    });
});

That change function isn't being invoked. I've also tried

$('#finished-checker').bind('change',function(){
    // ...
});

and I've tried putting these in $(document).readys and yada yada.

fiddle of proof: https://jsfiddle.net/c83cd6ah/

Upvotes: 0

Views: 38

Answers (1)

Ibrahim Khan
Ibrahim Khan

Reputation: 20740

Remove # from id of the checkbox finished-checkerUse and use ajax instead of post.

$.ajax({
    method: 'POST',
    url: '/Answers/UpdateFinishedValue',
    data: { finished: this.checked },
    success: function (retobj) {
        console.log(retobj);//TEST
    },
    error: function () {
        console.log("Error ...");
    }
});

Upvotes: 1

Related Questions