473,406 Members | 2,816 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,406 software developers and data experts.

Add EventListener to <a href...> tag

I want to clean my html from JavaScript and attach all EventHanding
code as:

document.getElementById('to').addEventListener('on Mouseout',delayhidemenu,false);
document.getElementById('s').addEventListener('onM ouseout',delayhidemenu,false);

that way, I can work with a clean HTML page and a separate JavaScript
file.

But, I can't make it work with links, having:

<SCRIPT LANGUAGE="JavaScript">
window.onload=function(){
document.getElementById('h').addEventListener('onC lick',say,false);
}
function say(){
alert("hello");
}
</SCRIPT>
<a id="h" href="#">hhh</a>

Produces the error: The object doesn't accept this property or method.

How can I add an event handler to a <a> tag?

Best Regards
Fabio Cavassini

Dec 15 '05 #1
8 8314
Fabio Cavassini wrote in
news:11*********************@g44g2000cwa.googlegro ups.com in
comp.lang.javascript:
I want to clean my html from JavaScript and attach all EventHanding
code as:

document.getElementById('to').addEventListener('on Mouseout',delayhideme
nu,false);
document.getElementById('s').addEventListener('onM ouseout',delayhidemen
u,false);

that way, I can work with a clean HTML page and a separate JavaScript
file.

But, I can't make it work with links, having:

<SCRIPT LANGUAGE="JavaScript">
window.onload=function(){
document.getElementById('h').addEventListener('onC lick',say,f
alse);
}
function say(){
alert("hello");
}
</SCRIPT>
<a id="h" href="#">hhh</a>

Produces the error: The object doesn't accept this property or method.

How can I add an event handler to a <a> tag?


<script type="text/javascript">
window.addEventListener(
'load',
function()
{
document.getElementById('h').addEventListener('cli ck',say,false);
},
false
);

function say()
{
alert("hello");
}
</script>

Note the lack of an 'on' prefix to the event names.

Internet Explorer will fail with the above as it (currently)
lacks addEventListener, it dose have a proprietry attachEvent
though:

<script type="text/javascript">
function add_listener( on, name, func )
{
if ( on.addEventListener )
{
on.addEventListener(
name.replace( /^on/, ''), func, false
);
}
else if ( on.attachEvent )
{
on.attachEvent( name, func );
}
else
{
on[name] = func;
}
}

add_listener(
window, 'onload',
function()
{
add_listener( document.getElementById('h'), 'onclick', say );
}
);
</script>

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Dec 15 '05 #2
Work perfect, thanks ;)

Year 2005 and still we have to take care of different JavaScript
browser implementations...that's embarrassing....

Regards
Fabio Cavassini

Dec 15 '05 #3

Fabio Cavassini wrote:
[snip]
Year 2005 and still we have to take care of different JavaScript
browser implementations...that's embarrassing....


You have to remember that IE is still stuck back in 2001, while other
browsers like Firefox and Opera are in continual development, which
allows them to keep up with the standards.

Chris Lieb

Dec 15 '05 #4
Is it possible to add parameters to this?

For example:

add_listener( document.getElementById('h'), 'onclick',
say(someStaticParameter) );

or

add_listener( document.getElementById('h'), 'onclick',
say(document.getElementById('h')) );

Best Regards
Fabio Cavassini
http://www.pldsa.com

Dec 22 '05 #5
Fabio Cavassini wrote:
Is it possible to add parameters to this?
To /what/?

<URL:http://jibbering.com/faq/faq_notes/pots1.html#ps1Post>
<URL:http://www.safalra.com/special/googlegroupsreply/>
For example:

add_listener( document.getElementById('h'), 'onclick',
say(someStaticParameter) );

or

add_listener( document.getElementById('h'), 'onclick',
say(document.getElementById('h')) );


I do not understand the question. What is add_listener(), what is say()?
PointedEars
Dec 22 '05 #6
> Is it possible to add parameters to this?

You'll need to make some tricks to achieve this...

Something like works fine:

getHandler = function getHandler(arg1, arg2){
return function(e){
alert([arg1, arg2].join("\n"));
};
}

addEvent(document, "mousedown", getHandler("Mouse was pressed out",
"mousedown"));
addEvent(document, "mouseup", getHandler("Mouse was released",
"mouseup"));

But if you need to remove the handler later, you'll need to do
something like:

addEvent(document, "mousedown", x = getHandler("Mouse was pressed out",
"mousedown"));
addEvent(document, "mouseup", y = getHandler("Mouse was released",
"mouseup"));

:

removeEvent(document, "mousedown", x);
removeEvent(document, "mouseup", y);
--
"Invente, Tente!!! Faça um código eficiente" (eu)

Jonas Raoni Soares Silva
---------------------------
jonasraoni at gmail dot com
http://www.jsfromhell.com

Dec 22 '05 #7
Fabio Cavassini wrote in news:1135267741.289210.81150
@z14g2000cwz.googlegroups.com in comp.lang.javascript:
Is it possible to add parameters to this?

For example:

add_listener( document.getElementById('h'), 'onclick',
say(someStaticParameter) );

or

add_listener( document.getElementById('h'), 'onclick',
say(document.getElementById('h')) );


A closeure would solve the problem:

add_listener(
document.getElementById('h'),
'onclick',
function ()
{
say(someStaticParameter);
}
);

If you want to capture the value that "someStaticParameter"
has at the point you call add_listener rather than the value
it will have at some point in the future when the event gets
fired, then you need another level of indirection:

function add_listner_capture_arg( obj, name, f, arg )
{
add_listener(
obj,
name,
function ()
{
f( arg );
}
);
}

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Dec 25 '05 #8
Thanks Rob!!!

It works perfectly, again

Thanks, slowly I'm getting taste of the flexibility of JavaScript....

Best Regards

Dec 26 '05 #9

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

Similar topics

2
by: Gregor Horvath | last post by:
Hi, Before I reinvent the wheel I`d like to ask if someone has done this before since I did not find an advice at Google. The goal is to create a dynamic Tree View in HTML. Say I have a...
4
by: matatu | last post by:
Hi to all, I have a xml file, a substring like: &lt;a href=&quot;#&quot;&gt;text&lt;/a&gt; which after an xslt trasform is rendered as (using xsl:output method html): &lt;a...
2
by: Donald Firesmith | last post by:
I am having trouble having Google Adsense code stored in XSL converted properly into HTML. The <> unfortunately become &lt; and &gt; and then no longer work. XSL code is: <script...
6
by: Lasse | last post by:
I have done this simple function, it seems to work as intended, to solve a problem i have had for a while. I couldnt find any sample around that was working for me. I would like to test it with...
3
by: TR | last post by:
Is it possible with CSS to prevent this wrapping alignment with a checkbox with a nested label? This is the label of the checkbox that wraps beneath it I'd prefer it looked like...
2
by: js | last post by:
I have a table rendered with XSLT. The first column has a radio button controls for user to make a selection for a particular row. All the values in the remaining columns are all concated with a...
11
by: Les Paul | last post by:
I'm trying to design an HTML page that can edit itself. In essence, it's just like a Wiki page, but my own very simple version. It's a page full of plain old HTML content, and then at the bottom,...
1
by: John | last post by:
Hi var poster="<html><head..... etc .... </html>"; var animal='dog'; The string contains images and text that changes. Originally I wanted to do something like print "<a href=" +...
1
by: mark4asp | last post by:
<form runat="server"automatically adds <divtag to code contained within. Is there a way to stop that? Mixing block-level elements with inline-level elements messes up the HTML becasuse that is...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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...

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.