473,405 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

One event handler provoking another

I'm seeing surprising behavior, consistent across Opera, Firefox, and IE.
An event handler is changing the value of an element, and there is apparently
no Change event being generated for the element. A simple illustration is,
make an INPUT element with an onchange; type in a new value, the Change
event handler is called. But make a button whose Click handler changes the
value, and the Change handler does not get called.

It appears that events are not generated while an event handler is running.
Can this be true?
Jul 23 '05 #1
3 1753

"Clarence Gardner" <cl******@silcom.com> wrote in message
news:12*************************@posting.google.co m...
I'm seeing surprising behavior, consistent across Opera, Firefox, and IE.
An event handler is changing the value of an element, and there is apparently no Change event being generated for the element. A simple illustration is,
make an INPUT element with an onchange; type in a new value, the Change
event handler is called. But make a button whose Click handler changes the
value, and the Change handler does not get called.

It appears that events are not generated while an event handler is running. Can this be true?


Under Mozilla, I achieved the desired effect by having the button first
focus the field to be changed,
then setting the new value, then changing the focus to another field.
It appears in this case to depend upon the browser's exact criteria for
triggering an onchange.
I.E. & Opera appear to insist upon the keyboard being involved, so you may
have to invent another
method to achieve what you want.
<HTML>
<BODY>
<!--Works with Mozilla-->

<FORM>
<INPUT value='0' type=text size=80 onchange='dfe[1].value+=" Top field
added me" '>&lt;- Change my
value, then blur me.<BR>

<INPUT type=text size=80 value=''><BR>

<INPUT type=button value="Change value of top field"
onmouseup='dfe[0].focus();dfe[0].value+=" Button

did this";dfe[0].blur()'><BR>

<INPUT type=reset value='Clear this mess'>
</FORM>

<SCRIPT>
var dfe=document.forms[0].elements;
</SCRIPT>

</BODY>
</HTML>
--
S.C.
Jul 23 '05 #2
Clarence Gardner wrote:
It appears that events are not generated while an event handler is running.
Can this be true?


It has nothing to do with whether the event handler is running or not.

Javascript may or may not fire events for things that happen programmatically.
Changing the value of an input programmatically does not trigger the onchange
event, issuing document.forms['formName'].submit() does not fire the onsubmit
event of the form (the exception being some versions of Opera, which _did_
incorrectly fire the onsubmit event of the form when calling it's submit()
method).

However, calling the click() method of a button causes that button's onclick
event to fire, and calling the focus() and blur() methods on an element both
cause the onfocus and onblur events to fire respectively.

Anyway, if you want to process the onchange event of an input after changing it's
value programmatically, the correct way to do it is as follows:

<script type="text/javascript">
function test(f) {
var theElement = f.elements['myInput'];
if (theElement) {
theElement.value = 123;
if (theElement.onchange) {
theElement.onchange();
}
}
}
</script>
<form name="myForm">
<input type="text" name="myInput" value="456"
onchange="alert('myInput=' + (+this.value +1));">
<input type="button" name="myButton" value="Test" onclick="test(this.form);">
</form>

Calling the onclick() method programmatically after changing the value
programmatically will have a much better chance of success then flipping the
focus around in an attempt to make the user agent trigger the event itself.

Of course, if you're already _in_ the onchange event when you do this, it would
be a good idea to include some way of preventing an endless loop, where the
onchange event changes the value, then calls the onchange event, which calls the
onchange event, etc.

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #3
Thanks, folks. Those were very good and helpful answers.

Before I saw them, I ended up changing the field from a regular text-type
to a radio button, and calling its click() method; I put the same code from
the text's onchange into the button's onclick.

Your answers made me realize why it didn't work in the first place, relating
to the standard's wording (paraphrased) that that onchange event is fired when
an element loses focus and its value is not the same as it was when it gained
the focus. I ASSUMED :(

Believe it or not, all I was actually trying to do was to get some code to
run in another window. I have window A loading a document into iframe B,
and A wants a function to run after B has loaded. It got convoluted because
of various things not working, (I don't remember the details). I wanted to
just call the function from the onload handler in B's document, but it's
apparently impossible to address a JavaScript function that way. (Even though
I regularly do so from one frame calling into another.) So I figured, if I
can't get to the function, I can get to an element over there that *can* get
to the function. Whew! Roundabout, but it works.

Thanks again for the great responses.
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Christopher W. Douglas | last post by:
I am writing a VB.NET application in Visual Studio 2003. I have written a method that handles several events, such as closing a form and changing the visible status of a form. I have some code...
8
by: Mark | last post by:
Hi, I'm looking for some ideas on how to build a very simple Event processing framework in my C++ app. Here is a quick background ... I'm building a multithreaded app in C++ (on Linux) that...
3
by: jeff29_b | last post by:
I am having a strange problem on a web form. I have an image button with an OnClick event handler. When I click the image the event isn't being called in the code behind when browsing in firefox....
15
by: Amit D.Shinde | last post by:
I am adding a new picturebox control at runtime on the form How can i create click event handler for this control Amit Shinde
13
by: Charles Law | last post by:
Mr "yEaH rIgHt" posted the following link about a week ago in answer to my question about removing event handlers. > http://www.vbinfozine.com/t_bindevt.shtml Following on from that post, the...
5
by: james | last post by:
Hello, I am having a little trouble creating an event handler for a context menu toolstripmenuitem. I've seen various tutorials and so on, but I keep getting a bit stuck! So far I have a second...
9
by: jeff | last post by:
New VB user...developer... Situation...simplified... - I want to wrap a pre and post event around a system generated where the pre-event will always execute before the system event and the...
1
by: tdan | last post by:
I do not know how to get Event.stopObserving() to work in the context I am using it. I am displaying a Color Selection Table and attaching 2 events: 1. onmouseover to display the color to the user...
1
by: kaczmar2 | last post by:
I have an ASP.NET page where controls are created dynamically, and I have an issue where one event handler creates another set of controls, and then adds event handlers to those controls. The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.