Marm
Marm

Reputation: 873

How to trigger an onChange event in HTML, even if the content is changed in js?

I know the HTML onChange event is triggered only when the element is blurred.

But is there a way to run a script when the content of a textbox changes? Even if it is changed in Javascript?

I have a textbox in my form to select a date. I also have a button with a calendar to pick a date. This calendar writes in the textbox and I want to do something when the date changes.

I cannot change the Javascript of the calendar.

I also want compatibility with all major browsers (IE7-9, Firefox, Chrome, Safari, Opera, ...).

Upvotes: 0

Views: 1045

Answers (2)

Marm
Marm

Reputation: 873

I found an answer to my question wich is maybe not the better one, but still works. It uses javascript timers.

function watchElement()
{
    var newVal = document.FormName.TextboxName.value;
    if(oldVal != newVal) {FunctionToCall();}
    oldVal = newVal;
}

var oldVal = document.FormName.TextboxName.value;
setInterval(watchElement, 500);

I know this is not the better code in the world, but it works for every major browsers.

Upvotes: 0

Samuli Hakoniemi
Samuli Hakoniemi

Reputation: 19049

You're looking for mutation events (eg. DomAttrModified).

See Detect element content changes with jQuery if that would help you as a starter.

Upvotes: 2

Related Questions