473,503 Members | 3,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trapping onChange event

I realize that the onChange event for a drop down can be trapped as
follows:

<select name="myDropDown" onChange="somefunc">

Is it possible to trap the onChange event outside of the select
statement? Something like this:

document.forms[0].myDropDown.onChange = somfunc;

Mar 22 '06 #1
3 3888
b_*****@yahoo.ca said on 23/03/2006 1:58 AM AEST:
I realize that the onChange event for a drop down can be trapped as
follows:

<select name="myDropDown" onChange="somefunc">
Maybe you mean:

<select name="myDropDown" onChange="somefunc()">

Is it possible to trap the onChange event outside of the select
statement? Something like this:

document.forms[0].myDropDown.onChange = somfunc;


That would assign a reference to somfunc to the _onchange_ (note
capitalisation) attribute of the select. When the selected option is
changed, somfunc is called.

Assigning an onchange (or any other 'on' event) effectively creates a
property of the element's DOM object, think of it like:

elObj = {
id : '',
name : 'myDropDown',
onchange : function(event){ somfunc(); },
onclick : function(event){ .... },
style : {
color: '...',
backgroundColor: '...'
},
/* ... lots more properties ... */
}
Where the script content of the HTML on* attribute is inserted as the
body of the anonymous function that is the value of the appropriate on*
property. The event parameter is automatically passed in the W3C model,
for IE you have to get it with window.event.

If that fits your definition of 'trap the onchange event' then yes, you
can do it.

However, if what you are trying to do is detect if the onchange event is
executed using some kind of global listener, no you can't. Whatever you
want to happen when the change event occurs must be called from the
onchange function.

Using shortcut property accessors as above is non-standard but widely
supported. It will cause somfunc to replace references to any functions
that might have been assigned to the onchange event previously. If you
want to add an extra function, say heyOnchangeFired(), then use
addEventListener (W3C event model) and attachEvent (IE event model).

Search the archives, there are many examples.

--
Rob
Mar 23 '06 #2
Thanks for the response.. I still can't get it to work.. Here's my
code:

Shouldn't testFunc be called when the I change the dd selection?

--------------

<html>

<head>
<script language="JavaScript">
function testFunc() {
alert("Hello");
}
</script>
</head>

<body>
<form>

<select name="dd">
<option selected>Select one</option>
<option>Dogs</option>
<option>Fish</option>
<option>Birds</option>
</select>

<script language="JavaScript">
document.forms[0].dd.OnChange = testFunc;
</script>

<hr>

</form>
</body>
</html>

Mar 23 '06 #3
b_*****@yahoo.ca wrote:
Thanks for the response..
Which response? Please use "Options, Reply" in Google Groups to
quote the minimum of what you are replying to.
[...]
Shouldn't testFunc be called when the I change the dd selection?
Yes, it should not. Besides the fact that your code is not Valid
(<URL:http://validator.w3.org/>), ...
[...]
<select name="dd">
<option selected>Select one</option>
<option>Dogs</option>
<option>Fish</option>
<option>Birds</option>
</select>

<script language="JavaScript">
document.forms[0].dd.OnChange = testFunc;
</script>


.... the property name is `onchange', not `OnChange', and the reference
should be document.forms[0].elements['dd']. However, you do not need
an additional script element with proprietary code here:

<select name="dd" onchange="testFunc(event);">
...
</select>
HTH

PointedEars
Mar 23 '06 #4

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

Similar topics

4
2943
by: Wugi | last post by:
I'm trying to find an equivalent of key-event trapping which is easy in QBasic. Several of my programs build up a geometric figure with two parameter line families, eg with two nested for..next...
1
20672
by: Covad | last post by:
Hi all, For some reason my change() function is only called when the page loads. I'd much rather it gets called when the select changes. Here's the code: window.onload = init; function...
2
1742
by: F. Da Costa | last post by:
Hi, I'm trying to call/ mimic an onchange function/ event after a user has clicked a checkbox. In response to which a buncch of other ones need to be checked as well. The caveat is that each of...
2
12113
by: Asit | last post by:
In JavaScripts checks for an onChange event against the value of the textbox at the time of the last onChange event. Since an onChange Event never fired after you changed the text first time ,...
13
31169
by: aundro | last post by:
Hello, I've been looking on the web for a solution to this problem: I create a set of checkboxes, and 2 buttons: - one is labeled "All" - the other is labeled "None" Clicking "All" is...
4
4068
by: Zeebra3 | last post by:
Here goes: I have a web form with several asp:dropdownlists, with which, when selection is changed I want to fire an event defined in some clientside js. The content of the clientside code is...
3
3960
by: jab3 | last post by:
Hello. I"m new to this group, and to JavaScript in general, so please forgive me if I breach local etiquette. I'm trying to implement some client-side 'dynamic' validation on a form. I'm having...
21
29738
by: Leena P | last post by:
i want to basically take some information for the product and let the user enter the the material required to make this product 1.first page test.php which takes product code and displays...
0
7192
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7064
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7261
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
5559
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3158
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3147
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1492
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
369
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.