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

Home Posts Topics Members FAQ

How to add extra action to onclick event ?

abs
My element:
<span onclick="alert( 'test')" id="mySpan">tes t</span>

Let's say that I don't know what is in this span's onclick event. Is it
possible to add another action to this element's onclick event ? I've tried
something like this:

oncl = document.getEle mentById('mySpa n').onclick
oncl = oncl + '\n;alert(\'add ed\')'
document.getEle mentById('mySpa n').onclick = oncl

And after this operation there'e no reaction to clicking on the <span>.
Anybody knows how to fix it ?

Best regards,
ABS


Jul 23 '05
17 4883
VK
> Bypass what?

I thought you couldn't do like obj.attachEvent ('onclick',
function(){aler t('Click!');})
But my test case appeared to be broken, it works, so sorry.

Jul 23 '05 #11
VK
I guess

foo = (arr[i].addEventListen er)?
arr[i].addEventListen er('click', extra, false) :
arr[i].attachEvent('o nclick', extra);

(where 'extra' is your function) would be enough in any context and no
problem with passing argument.

You have to remember that intrinsic handlers (<elm onevent="...") have
priority over programmed ones, so the function call goes like this:
1. intrinsic handlers
2. programmed handlers in random order (actually, this is the same
"organized disorder" as with key/value pairs in hash)

If you want to extend functionality based on the content of the
intrinsic handler, you may write an "EventSteal er" though it has more
relation to hacking rather than to development:

....
var objRef = e.target || event.srcElemen t;
var evtType = (e.type)? e.type : event.type;
var funRef = objRef['on'+evtType];
alert("On " + evtType + " this "+ objRef.tagName + " does this:\n\n" +
funRef.toString ();)
objRef['on'+evtType] = null;
alert("And now it does not!");

The code above is useless as it is, but you may study the function body
using RegExp and recompile it based on the study results.

Jul 23 '05 #12
On 27/05/2005 07:17, abs wrote:

The problem with the code as posted is that createDispatche r doesn't
actually return anything. I apologise.

After the line:

Dispatcher.deta ch = detachDispatche r;

add:

return Dispatcher;

After that it 'works', but I'll be happier once I perform more tests.
And another question: will I be able to pass the parameter to added function
?


I'm afraid not, but that's how the event model itself works. The
function you pass to the add method will always receive one argument:
the event object. The difference between this and using the event
attributes in HTML is that the user agent creates this outer function
for you. However, you can easily call other functions within the
listener (just like alert in the example), passing any arguments you like.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #13
abs
Thank you guys for your posts. You helped me much.

ABS
Jul 23 '05 #14
VK wrote:
I guess

foo = (arr[i].addEventListen er)?
arr[i].addEventListen er('click', extra, false) :
arr[i].attachEvent('o nclick', extra);

(where 'extra' is your function) would be enough in any
context and no problem with passing argument.
Except on browsers that support neither mechanism, where the call to the
non-existent - attachEvent - method errors and kicks the code out of
execution.

<snip> var objRef = e.target || event.srcElemen t;
var evtType = (e.type)? e.type : event.type;
If - e - was not defined evaluating the expression - e.type - would
produce an error (again). It makes more sense to normalise the event
object once with - e = e || window.event -(and the like) and then act
only on that normalised event object.
var funRef = objRef['on'+evtType];
alert("On " + evtType + " this "+ objRef.tagName +
" does this:\n\n" + funRef.toString ();)
objRef['on'+evtType] = null;
alert("And now it does not!");

The code above is useless as it is,
Absolutly.
but you may study the function body using
RegExp and recompile it based on the study results.


The - toString - method of functions is specified as producing an
"implementa tion dependent" string. That string does not necessarily
correspond with the function's source code, and cannot necessarily be
re-interpreted as a functionally equivalent function. I.E. This strategy
should be expected to fail, and it certainly will in known existing
environments under some circumstances.

Richard.
Jul 23 '05 #15
On 27/05/2005 12:20, Richard Cornford wrote:

[snip]
The - toString - method of functions is specified as producing an
"implementa tion dependent" string. That string does not necessarily
correspond with the function's source code,
Maybe, but I'd think not. After the specification states that "An
implementation-dependent representation of the function is returned", it
goes on to say that "This representation has the syntax of a
FunctionDeclara tion."

My interpretation is that the returned value should correspond with the
source code, but that the "placement of white space, line terminators,
and semicolons within the representation string is
implementation-dependent."

Of course, what should happen and what does happen in practice isn't
always the same.
and cannot necessarily be re-interpreted as a functionally equivalent
function.


Very true. There may be no way to reconstruct the scope chain, which may
render the function completely useless.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #16
Michael Winter wrote:
On 27/05/2005 12:20, Richard Cornford wrote: <snip>
The - toString - method of functions is specified as
producing an "implementa tion dependent" string. That
string does not necessarily correspond with the
function's source code,

<snip> it goes on to say that "This representation has the syntax
of a FunctionDeclara tion." <snip> Of course, what should happen and what does happen
in practice isn't always the same.

<snip>

Yes, regular expression literals within function bodies have proved a
recurrent problem in this context because they are independently
interpreted into regular expression objects. It is not unusual to find a
regular expression object in a toString-ed function coming out as
[Object], or something similar. Which is a syntactically fine expression
in a regular expression literal context (being an array literal with one
element referring to the Object constructor), but will certainly not
produce the original functionality.

Chopping up function body strings and re-assembling the results has been
tired, but all that I have observed trying it (at least those that
actually test their code more than superficially) seem to rapidly
abandon the idea in favour of accumulating function references in the
type of code you presented.

Richard.
Jul 23 '05 #17
VK wrote:
Bypass what?


I thought you couldn't do like obj.attachEvent ('onclick',
function(){aler t('Click!');})
But my test case appeared to be broken, it works, so sorry.


No. You both can and cannot use this. It depends on the JScript version
available on the client whether function expressions are considered a
syntax error or not. With new IEs, chances are high that it works.
PointedEars
--
Let us not judge others because of their religion, color or nationality.
We are all just human beings living together on this planet. (poehoe.de)
Jul 23 '05 #18

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

Similar topics

2
1568
by: Matthew | last post by:
Hi, how can I create a <A HREF'ed> image that, with an onclick event will cause the cursor to jump into a particularly assigned text form input field?
17
61445
by: Mike Gratee | last post by:
Is it possible to use JavaScript to cause the browser to click a link on a page and have the browser act exactly like the user had clicked on the link directly? In other words, I need to programmatically issue a JavaScript statement which causes the browser to act just like the user clicked on a link in my page. And if that link has an onClick JS event defined, I'd want that onClick event to execute too, exactly the same as if the user...
2
3424
by: Vinita Sharma | last post by:
Hi All, I have a strange problem. I have 2 text boxes and a button in my form. There is a function called on onchange event of the first text box. There is another function called on onclick event of the button. Things work fine if you move from one field to another using tab keys. But if you change something in the first text box and move to button using mouse, the onchange event of the text box is called but the onclick event of the...
3
3810
by: f1crazed | last post by:
Hello, I am wanting to fire the onClick event of button1 by pressing button2. Does anyone have a clue if this is even posible? If so PLEASE HELP!! Thanks.
4
1968
by: Roland | last post by:
I was wondering, what happens when you have an onclick event and an error occurs in it: In an <a> element: onclick="zoomFullExtent(); return false;" I know that there is an error happening in zoomFullExtent. I didn't define my own error handler, so the default one is used.(My browser is Firefox 1.0).
5
13963
by: Stuart Shay | last post by:
Hello All I am working on ASP.NET 1.1 Custom Pager that allows a User to Enter a Number in a TextBox and go to the page selected. Since the OnClick Event does not work in ASP.NET 1.1 for a TextBox I want to use a hidden button to fire when the Onclick Event is fired for the TextBox.
3
6509
by: Michael_R_Banks | last post by:
I'm trying to dynamically build a table that allows users to remove rows when they click a corresponding button. For some reason, whenever I add the button to the table, it never fires the onclick event. I'm stumped with this one, any assistance would be appreciated. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:// www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"...
7
13223
by: Moses | last post by:
Hi Everybody, I have a problem with onClick event which works in FF and does not work in IE, Here I have giving the details Please help. I am creating a <aTag. dom_obj = document.createElement('a'); dom_obj.setAttribute('href', 'javascript:void(0)'); dom_obj.setAttribute('onclick', 'javascript:test()');
6
2178
by: Nathan Sokalski | last post by:
I have a DataList which contains several LinkButtons, which are used to select a category in my application. I want the currently selected category to use a different CSS class. Here is an example of the generated code for one of the buttons: <a onclick="UnselectCategories();datCategories_ctl00_lnkCategory.className='Category_Selected';" id="datCategories_ctl00_lnkCategory" class="Category_Selected"...
0
9646
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...
1
10097
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
9957
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8983
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...
1
7505
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6742
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
5386
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...
0
5518
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3658
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.