3

I have some object (div, input/checkbox) in an HTML page. The values for their "checked" and "disabled" attributes are set via some JS functions. I want to listen to changes of these attributes, but couldn't find any function or listener to perform that.

How can I add an event listener that listen on changes of "checked" or "disabled" and execute some code (e.g. changing style of checkbox / div) depending on the attributes' status?

Thanks

Konrad
  • 4,329
  • 10
  • 54
  • 88
  • 1
    For changing style, you might want to simply use the `:checked` pseudoclass in CSS: http://jsfiddle.net/ceEnA/. – pimvdb Nov 25 '11 at 10:09
  • I change no CCS styles but add attributes like "disabled" to a div – Konrad Nov 25 '11 at 10:16
  • Not sure what you're after, but "(e.g. **changing style of checkbox / div) depending on the attributes' status**" can be most easily implemented with CSS. – pimvdb Nov 25 '11 at 10:18
  • 1
    To expand on what @pimvdb said, you can also used the `:disabled` pseudoclass - I've done a (rather crude) update to pimvdb's fiddle: http://jsfiddle.net/ceEnA/2/ – nnnnnn Nov 25 '11 at 10:19

6 Answers6

3

Regarding the "disabled" change, you may be able to listen to DOMAttrModified events. See this test case for details: http://jsfiddle.net/4kWbp/1/

Note that not all UAs support DOM mutation events like DOMAttrModified, and in those that do support them, listening to them may cause performance to detoriate.

Setting .checked directly does not trigger "change" events, and doesn't seem to trigger DOMAttrModified either (only tested in Opera though, and this is the sort of under-specified between-the-spec-gaps stuff that might well be very inconsistent across browsers. Perhaps it's an Opera bug.)

The last resort would perhaps be defining getters/setters for those properties. That would be a rather ugly hack though..

hallvors
  • 6,069
  • 1
  • 25
  • 43
  • 2
    Heh, I see Chrome does NOT fire DOMAttrModified in this test. Firefox fires fewer events than Opera, so less consistent. Oh well. So the best way to do this is to hook into or modify the script that sets .checked and .disabled to make it send some event you can listen to or call your JS directly. Is that not an option at all for your project? – hallvors Nov 25 '11 at 10:42
1

You can use the pseudo-selectors for :checked and :disabled and trigger an empty animation which causes an animationstart event. For example:

select > option:checked {
    animation: checked 1ms;
}
select > option:not(:checked) {
    animation: unchecked 1ms;
}
@keyframes checked { from {}; to {}; }
@keyframes unchecked { from {}; to {}; }

and then in javascript you can use:

document.body.querySelector('select').addEventListener('animationstart',
    function (ev) {
        console.dir(ev);
    });

The event has available animationName and target properties which give you enough to work with.

Parakleta
  • 1,121
  • 10
  • 19
0

$('#Checkboxid:checked') this will return true if checkbox value is on and false if checkbox value is off. this is jquery selector function :checked can be use to check radio and checkbox values.

syntax is as follows:

$('#checkboxid:checked')
Mike
  • 118
  • 1
  • 3
  • 11
  • 2
    But I don't want to select all checked checkboxes, but I want to react on the change of this status ... How can I add a listener to the "checked" status? – Konrad Nov 25 '11 at 10:17
0

To 'listen' to when a checkbox has been toggled use change():

$('.target').change(function() {
    console.log(this.checked ? 'Checked' : 'Not Checked');
});
StuR
  • 12,042
  • 9
  • 45
  • 66
  • 2
    The "change"-function only fires, if the checkbox selection was changed by a direct click and not via script, e.g., using $(checkboxID).attr('checked', 'checked'); – Konrad Nov 25 '11 at 10:32
0

Use the :checked to determine the checked radio or checkbox on the page or container.

First of all check this jsfiddle

after this follow this stackoverflow question Jquery get selected checkboxes to write for your desired result.

you can check disabled or not as:

var set=1;
var unset=0;
jQuery( function() {
  $( '.checkAll' ).live('click', function() {
    $( '.cb-element' ).each(function () {
      if(set==1){ $( '.cb-element' ).attr('checked', true) unset=0; }
      if(set==0){ $( '.cb-element' ).attr('checked', false); unset=1; }
    });
    set=unset;
  });
});

Check these links for detail:

Jquery get selected checkboxes

Setting "checked" for a checkbox with jQuery?

Community
  • 1
  • 1
Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75
-1

I don't have answer to your question, but I could suggest you to try something like Event Bus (in GWT is Event Handler).

For JQuery you can use Events.

Valchev
  • 1,490
  • 14
  • 17