473,387 Members | 1,611 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,387 software developers and data experts.

Common client side EventHandler

Hello,

Serverside I'm generating a html page.
There are different controls for which I want to create an
eventhandler manually.

Like:

document.forms[0].TextBox1.onchange = EventHandler;
document.forms[0].Button1.onclick = EventHandler;

function EventHandler(){
// do something
}

What I would like is to pass arguments to the Eventhandler.

Like:
document.forms[0].TextBox1.onchange = EventHandler('test1');
document.forms[0].Button1.onclick = EventHandler('test2');

function EventHandler(inp){
// do something conditioned by parameter.
}
This sample is simplified, additional, in the EventHandler I'll
execute the original eventhandlers which I'm gonne store in a
variable.
So, the question:

Is it possible to have an eventhandler with parameters? or should I
create an eventhandler for all controls which need some extra dynamic
behavior?

By the way, Yes, I know I can simply use <input type="text"
id="TextBox1" onclick="DoFunction();" />
But that's not what I want in this case.
Thanks in advance!

Remco
Jul 23 '05 #1
3 3041
Remco wrote:
Hello,

Serverside I'm generating a html page.
There are different controls for which I want to create an
eventhandler manually.

Like:

document.forms[0].TextBox1.onchange = EventHandler;
document.forms[0].Button1.onclick = EventHandler;

function EventHandler(){
// do something
}

What I would like is to pass arguments to the Eventhandler.

Like:
document.forms[0].TextBox1.onchange = EventHandler('test1');
document.forms[0].Button1.onclick = EventHandler('test2');


You cannot pass additional arguments to your event handler as the call
will be generated by the browser. However, you may set some additional
properties for your object and check the event.target for that property.

eg:

var elm = document.forms[0].TextBox1;
elm.param = "foo";
elm.onchange = EventHandler;

function EventHandler(e){
e = e || window.event;
var target = e.target || e.srcElement;
if (target) {
param = target.param;
if (param == "foo") {
// do it
}
}
}

Daniel
Jul 23 '05 #2
On 2 Dec 2004 03:39:48 -0800, Remco <re*****@hotmail.com> wrote:

[snip]
Is it possible to have an eventhandler with parameters? or should I
create an eventhandler for all controls which need some extra dynamic
behavior?

By the way, Yes, I know I can simply use <input type="text"
id="TextBox1" onclick="DoFunction();" />
But that's not what I want in this case.


Yes, by emulating what happens when you add a listener via an intrinsic
event attribute.

When a user agent (that supports scripting) encounters

<element onclick="...">

it creates an anonymous function and inserts your code into it:

elementRef.onclick = function(event) {
/* ... */
};

You can do the exact same thing. Instead of writing

element.onclick = handler('...');

use

element.onclick = function(evt) {
handler('...');
};

Be aware that if 'handler' needs a reference to 'element', you'll have to
pass it as an argument. Similarly, if you want the event object, you'll
have to pass that too after changing the above to:

element.onclick = function(evt) {
/* IE doesn't pass the event object to
* listeners - it exists as a global
*/
evt = evt || event;
handler('...');
};

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #3
> Is it possible to have an eventhandler with parameters? or should I
create an eventhandler for all controls which need some extra dynamic
behavior?
The simple answer is not directly.

When an event handler is called, it can have no parameters. In fact,
other than IE, an event handler is called with just one parameter -
the "event" object itself. E.g. for cross-browser event handling:-

myElement.onlick=function(eEvent)
{
var eEvent=window.event||eEvent;

// Do something.
};

If you want to feed parameters to an event handler then there are two
basic ways to do this.

1. Use global level variables. I.e. variables defined at the global
level of your script.

<script>
var myParam=1;
myElement.onlick=function(eEvent)
{
var eEvent=window.event||eEvent;

alert(myParam);
};

</script>

2. Add properties to the element itself.

myElement.aProperty=1;

myElement.onclick=function()
{
function Display(eL)
{
alert(eL.aProperty);
}

Display(this);
};

3. Use "closures" (of which global level variables are an example in
fact).

Generally a variable defined in a parent function is available to a
sub-function.

Thus:-

<script>
function Parent()
{
var nParentVar=1;

function Child()
{
alert(nParentVar);
}

}

Parent();

</script>

With this you can prime functions with objects (called "currying").

<html>
<head>
<script>

function Initialise(eElement)
{
var oStyle=new Object;

eElement.onclick=function()
{
eElement.style.backgroundColor=oStyle.sColor;
};

return oStyle;
};

function Start()
{
var eDiv=document.getElementById("myDiv");
var oStyle=Initialise(eDiv);
oStyle.sColor="red";
}

</script>
</head>
</body>
<p onclick="fStart(this)">Click to initialse DIV handler</p>

<div id="myDiv" style="width:100; height:100; border-width:1;
border-style:solid; border-color:black;"></div>

</body>
</html>

By the way, Yes, I know I can simply use <input type="text"
id="TextBox1" onclick="DoFunction();" />
But that's not what I want in this case.


In fact, what is happening is that "DoFunction()" is wrapped in an
anonymous function.

Equivalent to:

TextBox1.onlclick=function()
{
DoFunction();
};
Jul 23 '05 #4

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

Similar topics

7
by: Sherry Littletree | last post by:
Hi All I am working on a site that has a large amount of common html on all its web pages. I am looking for a way to place this in a single file so, if changes are made, I can change this...
1
by: Jim Hammond | last post by:
I can get data from a client-side assembly to the server in two manual steps, but I need to be able to do it in one step. Step 1: The user presses the manually coded "Step 1" button, which calls...
5
by: Gary Vidal | last post by:
I have a client side Javascript which checks an OrderQuantityField against a hidden Textbox of the Minimum Order Quantity. I dont want to do validation on a postback. I would like to be able to...
2
by: Wysiwyg | last post by:
I'm going back to a previous asp.net (C#) web project after a few months of inactivity and my first form, the login, won't submit. I ran with the debugger and still can't see how to resolve this. I...
5
by: ezelasky | last post by:
I am trying to get a client side javascript function to invoke when the selection changes in a drop down list box. I have tried : ddl_specs.Attributes = "javascript"; ddl_specs.Attributes =...
0
by: Martin | last post by:
In server-side code of Page1.aspx, in the click eventhandler for an imagebutton, I branch to another page: Response.Redirect("Page2.aspx") But the client-side javascript on Page2.aspx is...
4
by: Olivier Matrot | last post by:
Hello, I have a problem with an ASP.NET 2.0 Application. A client request is processed in parrallel by two threads. This ends with the following exception : <Source>System</Source> <StackTrace...
0
by: Nathan Sokalski | last post by:
I have written a Validator (a class that inherits from the BaseValidator class), and it includes client-side validation functions. The validator is a conditional validator that only validates if a...
1
by: Nathan Sokalski | last post by:
I want to add a client-side onchange event to the input tag generated by a CheckBox Control. However, because the CheckBox generates the following set of tags: ...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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...

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.