Tuesday, 15 June 2010

javascript - How can I trigger an onchange event manually? -



javascript - How can I trigger an onchange event manually? -

this question has reply here:

how trigger event in javascript? 6 answers

i'm setting date-time textfield value via calendar widget. obviously, calendar widget :

document.getelementbyid('datetimetext').value = date_value;

what want : on changing value in date-time textfield need reset other fields in page. i've added onchange event listener datetimetext field not getting triggered, because guess onchange gets triggered when element gets focus & value changed on losing focus.

hence i'm looking way manually trigger onchange event (which guess should take care of checking value difference in text field).

any ideas ?

there's couple of ways can this. if onchange listener function set via element.onchange property , you're not bothered event object or bubbling/propagation, easiest method phone call function:

element.onchange();

if need simulate real event in full, or if set event via html attribute or addeventlistener/attachevent, need bit of feature detection correctly fire event:

if ("createevent" in document) { var evt = document.createevent("htmlevents"); evt.initevent("change", false, true); element.dispatchevent(evt); } else element.fireevent("onchange");

javascript

No comments:

Post a Comment