Fire an event from javascript!

H

Hans

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
 
R

Randell D.

Hans said:
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
 
R

RobB

Hans said:
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...
 
M

Michael Winter

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top