473,511 Members | 16,888 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Fire an event from javascript!

If I for example have two anchor tags

<a id="anchor1" onclick=".....">Link1</a>
<a id="anchor2" onclick=".....">Link2</a>
In a javascript function I get the id of an anchor tag and I want to execute
the onclick event (or some other event on this element). Is it possible?

I would like something like this
document.getElementById("anchor1").onclick

Regards
/Hans
Jul 23 '05 #1
3 19002
Hans wrote:
If I for example have two anchor tags

<a id="anchor1" onclick=".....">Link1</a>
<a id="anchor2" onclick=".....">Link2</a>
In a javascript function I get the id of an anchor tag and I want to execute
the onclick event (or some other event on this element). Is it possible?

I would like something like this
document.getElementById("anchor1").onclick

Regards
/Hans


I know there is a special object called 'this' which might prove helpful
here - but since I'm a newbie I've only got so far in using it in the
past - Perhaps though it might give you some direction to run with... If
I were to try it, I would use it something like so:

<a id="anchor1" onClick="return myfunction(this);">Link1</a>
<a id="anchor2" onClick="return myfunction(this);">Link2</a>

and....

function myfunction(ourObjectTag)
{
alert(ourObjectTag.name);
// or is it
alert(ourObjectTag.name.value);
return;
}

Its just an idea - I hope I'm not sending you on the wrong track but I'd
be curious of the solution when you work it out...

randelld
Jul 23 '05 #2
Hans wrote:
If I for example have two anchor tags

<a id="anchor1" onclick=".....">Link1</a>
<a id="anchor2" onclick=".....">Link2</a>
In a javascript function I get the id of an anchor tag and I want to execute the onclick event (or some other event on this element). Is it possible?
I would like something like this
document.getElementById("anchor1").onclick

Regards
/Hans


document.getElementById("anchor1").onclick();

Since 'onclick' is an object property of Link (Link.onclick), and
contains whatever string you assigned in HTML (inside a function
wrapper, applied automatically), you can invoke it like any other
function, by name. Same with handlers registered via script (including
function pointers). Keep in mind: this doesn't synthesize the actual
event (btw, it's CLICK, 'on' denotes the handler property) but simply
calls the handler. The Link.click() method will do the latter, but has
some compatibility issues.

DOM Level 2 has expanded capabilities in this area, someone will
doubtless mention them here...

Jul 23 '05 #3
RobB wrote:

[snip]
DOM Level 2 has expanded capabilities in this area, someone will
doubtless mention them here...


I take it you're referring to event simulation with the DOM Events
module, which would go something like:

var e = document.createEvent('MouseEvents'),
a = document.links['anchor1'];

e.initMouseEvent('click', /* Event type */
true, /* Can bubble */
true, /* Cancelable */
document.defaultView, /* View */
1, /* Mouse clicks */
0, /* Screen x */
0, /* Screen y */
0, /* Client x */
0, /* Client y */
false, /* Ctrl */
false, /* Alt */
false, /* Shift */
false, /* Meta */
0, /* Button */
null); /* Related target */

a.dispatchEvent(e);

Yes, in theory you could do that, however it's not likely it will "do"
anything. Any event listeners attached to the document (both capturing
and non-capturing) will be fired, but the default action associated
with the event is not likely to occur. This prevents a malicious
script from faking a mouse click to open a pop-up window or submit a
form, for example.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #4

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

Similar topics

2
1875
by: Kevin Ly | last post by:
Consider the test case below. The onmouseout event does NOT fire when my mouse/cusor is moved off the left side of the browser. It does if it is moved off the top, bottom, and the right side that...
5
6784
by: Carlo Marchesoni | last post by:
From an aspx page (A.aspx) I open another one (B.aspx - for table lookup). When the user selects an entry in B.aspx I would like to force a button's event in A.aspx to be fired. I guess the only...
4
5528
by: Carlo Marchesoni | last post by:
I have a button that opens a new Window (some kind of search-window), which is fired using JavaScript (btnSearch.Attributes=".....";) Now I need to run some code behind code BEFORE this JavaScript...
0
2473
by: jonathan.beckett | last post by:
I have been working on a client project recently that is using winforms ..NET user controls within web pages in Internet Explorer, and need to find out how to make the user control communicate back...
3
4737
by: rfinch | last post by:
I'm trying to pass two values from client script (Javascript) to the code-behind for a page using three hidden fields - two to hold the values and a third to fire the ValueChanged event. I have...
19
4722
by: Daniela Roman | last post by:
Hello, I try to fire an event under a button click event and maybe anybody can give a clue please. I have let's say a WEB grid with PageIndexChanged event: private void...
2
3717
by: jyanmin.fang | last post by:
Hi, In my current project, I need to embed an .NET winform usercontrol in the aspx page (via <Objecttag). This winform usercontrol has an event called DoEvent (void DoEvent()). This winform...
2
3899
by: John Kotuby | last post by:
Hi guys, I am converting a rather complicated database driven Web application from classic ASP to ASP.NET 2.0 using VB 2005 as the programming language. The original ASP application works quite...
2
10709
by: John Kotuby | last post by:
Hi all, In ASP.NET 2.0 and VB.NET, I am trying to get the OnSelectedIndexChanged event to fire a Javascript function. There is no OnClientClick event for that control. When I try something...
3
2794
by: Crazy Cat | last post by:
Hi all, I am developing an asp.net 2.0 application in Visual Studio 2005. On my page I have a simple datalist that is bound programmatically to a collection of simple objects. On this page I...
0
7242
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
7138
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
7355
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
7423
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
7510
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...
0
5668
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,...
1
5066
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...
0
3225
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
447
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.