sunnuntai 24. maaliskuuta 2013

Inverting checkbox selections with JQuery


I was building my first X3DOM test case and I needed to invert a checkbox selection with JQuery.  I found several ways of doing this that worked with previous versions but not with 1.9.1.

My first attempt was this ( inside .each -function):
$(this).attr("checked",!$(this).attr("checked")); 
That didn't work. Next attempt:
 $( this ).attr( 'checked', $( this ).is( ':checked' ) ? '' : 'checked' );
Nope. Then this:
$(this).is(':checked') ? $(this).removeAttr('checked') : $(this).attr('checked','checked');
Then I found it. Just click it with JQuery:

$(this).trigger('click');

Nice and simple!

Ei kommentteja:

Lähetä kommentti