473,623 Members | 2,473 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.oncha nge = EventHandler;
document.forms[0].Button1.onclic k = EventHandler;

function EventHandler(){
// do something
}

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

Like:
document.forms[0].TextBox1.oncha nge = EventHandler('t est1');
document.forms[0].Button1.onclic k = EventHandler('t est2');

function EventHandler(in p){
// 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="DoFunc tion();" />
But that's not what I want in this case.
Thanks in advance!

Remco
Jul 23 '05 #1
3 3063
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.oncha nge = EventHandler;
document.forms[0].Button1.onclic k = EventHandler;

function EventHandler(){
// do something
}

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

Like:
document.forms[0].TextBox1.oncha nge = EventHandler('t est1');
document.forms[0].Button1.onclic k = EventHandler('t est2');


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*****@hotmai l.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="DoFunc tion();" />
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.oncl ick = 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.onlic k=function(eEve nt)
{
var eEvent=window.e vent||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.onlic k=function(eEve nt)
{
var eEvent=window.e vent||eEvent;

alert(myParam);
};

</script>

2. Add properties to the element itself.

myElement.aProp erty=1;

myElement.oncli ck=function()
{
function Display(eL)
{
alert(eL.aPrope rty);
}

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(nParentVa r);
}

}

Parent();

</script>

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

<html>
<head>
<script>

function Initialise(eEle ment)
{
var oStyle=new Object;

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

return oStyle;
};

function Start()
{
var eDiv=document.g etElementById(" myDiv");
var oStyle=Initiali se(eDiv);
oStyle.sColor=" red";
}

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

<div id="myDiv" style="width:10 0; 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="DoFunc tion();" />
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.onlcli ck=function()
{
DoFunction();
};
Jul 23 '05 #4

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

Similar topics

7
5064
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 single file and do not have to change each and every page. I have the Java scripting in a common .Js file but have not been able to find a way to do this with my html content.
1
2671
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 the JavaScript function. The function copies the data into a server textbox control. Step 2: The user presses the VS generated "Step 2" button, which calls the
5
2152
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 have FormValidate Javascript function postback to my addToCart Method in my code behind function ValidateForm(myform) { if (myform.OrderQuantity.value == 0) { alert("Form is Invalid = 0 " + myform.OrderQuantity.value); return false } if...
2
2096
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 am developing with VS2003 and on Windows 2000 Server and have all of the service packs for .NET and Windows. The application is running on an intranet and I'm running this test with IE6. Javascript is functional. The logon form has text fields...
5
3657
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 = "return OnChangeSpec();"; with & without the "return"; and with & without AutoPostBack = true on the list box property and the javascript function just will not execute.
0
1567
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 breaking because it cannot find my function.
4
2699
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 at System.Collections.Specialized.ListDictionary.Add(Object key, Object value) at System.Web.UI.ClientScriptManager.RegisterScriptBlock(ScriptKey key, String script, ListDictionary&amp; scriptBlocks, ArrayList&amp; scriptList,
0
1020
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 specified checkbox, radiobutton, dropdownlist item, etc. is checked or selected. The only feature I have left to add is to hide the error message if they uncheck or unselect the checkbox or radiobutton by use of the onchange eventhandler. Is...
1
1046
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: <span><input/><label></label></span> If I try to add the onchange attribute to the CheckBox Control it is added to the span tag instead of the input tag (I understand why, so there is no need to explain that). The only way I can think of to add a client-side event handler to...
0
8227
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8165
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8670
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8326
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7150
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5561
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4074
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2602
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 we have to send another system
1
1778
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.