D
D
Denis Ilinykh2011-07-13 16:43:24
JavaScript
Denis Ilinykh, 2011-07-13 16:43:24

How to trigger an event on an element from javascript?

There is a checkbox on it, a function hangs on the onchange event.
When the checkbox state changes from the script through .attr("checked","checked"), the event does not fire.
Tell me how to change the element so that the event fires, or how to forcefully report that the element has changed.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
A
Anton, 2011-07-13
@sHinE

$(element).trigger('change');

V
Voenniy, 2011-07-13
@Voenniy

try $(element).click(); or $(element).change();

A
Anatoly, 2011-07-13
@taliban

javascript.gakaa.com/document-createevent-4-0-5-.aspx

I
itlife, 2011-07-14
@itlife

There are such pebbles underwater
1. $(element).click();
- The checkbox will change the state in all browsers, but the change event will not work in Opera, which in other browsers fires after click ... distinguished himself... but I don't remember exactly... ) In my case, $(element).click().change() satisfied if I didn't worry about double triggers. If the change event is not important... $(element).click(); is sufficient.
3.After changing the checkbox dynamically, it becomes unexpectedly difficult in js to check whether it is 1 or 0 turned out ... When checking the attr('checked') attribute or the checked property of a DOM object, they give different results in some browsers. If you check during the click, it is likely that there is a discrepancy due to the fact that some browsers change the state of the checkbox before the click, others after the click. If you check the state of the checkbox in the click handler, then you can stumble upon different results in different browsers, but this is just a suspicion ... since all the glitches have disappeared using the jquery selector value=$(element).is(':checked'); which is what I recommend :)

K
kastigar, 2011-07-13
@kastigar

In jQuery, any event can be triggered using the .trigger() function

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question