By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,238 Members | 1,661 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,238 IT Pros & Developers. It's quick & easy.

Fire an event from javascript!

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.