Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html checkBox onchange not working

<input class="jProblemClass" id="Checkbox{%= ID %}" type="checkbox" onchange="problemPicker.onChangeProblemCheckBox('{%=ID %}');"/>

After first check or uncheck nothing happens. Afetr second click, call my function problemPicker.onChangeProblemCheckBox, but i get ID first check. Why? Can help me anybody?

onChangeProblemCheckBox: function(id) {
    if ($('#CheckBox' + id).attr("checked") == true) {
        problemPicker.addToCheckProblems(id);

        var checkboxes = $('.jProblemClass:checked');

        if ((checkboxes.length > general.limit) && (general.limit != 0)) {
            alert('The limit of ' + general.limit + ' problems exceeded. Please deselect ' + (checkboxes.length - general.limit).toString() + '.');
        }
    } else {
        problemPicker.delFromCheckProblems(id);
    }
},
like image 570
isxaker Avatar asked Feb 09 '11 07:02

isxaker


People also ask

Does Onchange work for checkbox?

The onchange event occurs when the value of an element has been changed. For radiobuttons and checkboxes, the onchange event occurs when the checked state has been changed.

How do I keep a checkbox checked in HTML?

The checked attribute is a boolean attribute. When present, it specifies that an <input> element should be pre-selected (checked) when the page loads. The checked attribute can be used with <input type="checkbox"> and <input type="radio"> . The checked attribute can also be set after the page load, with a JavaScript.

How do you handle a checkbox in HTML?

The <input type="checkbox"> defines a checkbox. The checkbox is shown as a square box that is ticked (checked) when activated. Checkboxes are used to let a user select one or more options of a limited number of choices.


2 Answers

Bro use onclick event instead onchange.. Reason... According to the Javascript references I've read, onchange fires after the focus is lost. Unless you click somewhere else, the focus is not lost in IE. The other browsers must not be waiting for the focus to be lost before firing onchange - which suggest they are not following the spec correctly (despite it making more sense to fire at that point). How about using onclick and onkeydown/onkeyup instead (by using both onclick and onkeyup/onkeydown you cover both mouse and keyboard setting of the checkbox). REFERENCE : http://www.winvistatips.com/re-onchange-event-checkbox-not-working-properly-t311364.html

like image 71
Tami Avatar answered Oct 16 '22 12:10

Tami


I think, the problem goes from here: $('#CheckBox' + id).attr("checked") == true. Accordingly to HTML specs, checked should be set to "checked" when this event fires. So, try using something like $('#CheckBox' + id).attr("checked") == "checked" or even $('#CheckBox' + id).attr("checked").

As a second option, i recommend you to use pure jquery to run your routine. For example, if you have <input type="checkbox" id="ac"> checkbox, you can use this jq code to handle your routines:

$(document).ready(function() {
    $("input[type=checkbox]").change(function() {
        alert("Am i checked? - " + $(this).attr("checked") + "\nMy ID:" + $(this).attr("id"));
    });
});

This case is shown in this demo.

like image 28
shybovycha Avatar answered Oct 16 '22 10:10

shybovycha