views:

53

answers:

2

this is my html page

<script>
function set()
{
document.getElementById("txt").value++;
}
</script>
<input id="txt" type="text" onchange="javascript:alert('txt changed');" value="0">
<br/>
<input type="button" onclick="set()" value="Set Data"/>

when i press the button, the text box value is changing, and onchange even is not firing but when i enter value manually in the textbox, it is firing the onchange.

all i want is to call a javascript function when data is changed in the textbox when a button is clicked

please help?

+1  A: 

you can fire onchange from code using document.getElementById("txt").onchange(); after the changing the value through code

UPDATE

change your set function to take the id of the text box

function set(textboxid)
{
    var tb = document.getElementById(textboxid);
    tb.value++;
    tb.onchange();
}
Vinay B R
thats a quick reply, but my problem is like i have 5-6 textboxes with separate buttons for each. in that case i have to call this in each and every button click. any other alternatives
Harsha
check the update
Vinay B R
A: 

This appears to be a browser bug. With firebug I have logged all events in input. ANY events is triggered when you change value with function set();

I've tested in firefox.


Note: if it isn't a bug, at least is a wrong definition about how events works.

Topera