onchange event problem !!!

B

Brami Yair

below is example HTML code:
<input type=text onchange="javascript:alert('text change');">"

When i type something into the box, the onChange event fires,
but, when i change this box in code, the onChange event should fire but it doesn't.

why, and what i can do ?
 
M

Mick White

Brami said:
below is example HTML code:
<input type=text onchange="javascript:alert('text change');">"

<input type=text onchange="alert('text change');">

Mick
 
G

Grant Wagner

Mick said:
<input type=text onchange="alert('text change');">

Mick

If you change the value programmatically, then you have to call the "onchange()" method
of the element you are changing programmatically as well:

<form name="myForm">
<input type="text"
name="myInput"
value="old text"
onchange="alert('text changed');">
<input type="button"
value="Change text"
onclick="changeInput('new text', this.form.elements['myInput']);">
</form>
<script type="text/javascript">
function changeInput(s, element) {
element.value = s;
// the next line fires the onchange event of the element
element.onchange();
}
</script>

--
| Grant Wagner <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
* http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html
* Internet Explorer DOM Reference available at:
* http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp
* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-developer/upgrade_2.html
 
B

Brami Yair

Grant Wagner said:
Mick said:
<input type=text onchange="alert('text change');">

Mick

If you change the value programmatically, then you have to call the "onchange()" method
of the element you are changing programmatically as well:

<form name="myForm">
<input type="text"
name="myInput"
value="old text"
onchange="alert('text changed');">
<input type="button"
value="Change text"
onclick="changeInput('new text', this.form.elements['myInput']);">
</form>
<script type="text/javascript">
function changeInput(s, element) {
element.value = s;
// the next line fires the onchange event of the element
element.onchange();
}
</script>

--
| Grant Wagner <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
* http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html
* Internet Explorer DOM Reference available at:
* http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp
* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-developer/upgrade_2.html

thanks :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top