JQuery / javascript different actions with user click and programmatically check the box

A bit hard to explain, so I set up jsFiddle here .

Essentially, I have some actions when the user clicks the checkbox. Elsewhere, I'm trying to programmatically check a box, and I need to see exactly the same behavior. This does not work, it seems that something is related to the order of clicking and the definition of the checked attribute.

How to get the behavior I'm looking for without resorting to hacking?

+3
source share
3 answers

change ( jQuery , blur), :

$('span').click(function() { $('.test').click().change(); });

... , .

+2

javascript ... html:

<input type="checkbox" id="myid" />&nbsp;<label for="myid">My Label</label>

javascript-, .

:)

jquery:

$('span').click(function() { $('.test').trigger('click'); });
+1

, , javascript click jQuery, :

$('.test')[0].click();
0

Source: https://habr.com/ru/post/1779988/


All Articles