Calling a function in the userscript, from a inserted element

2759 views
Skip to first unread message

Stian Hole

unread,
Oct 7, 2007, 6:21:15 AM10/7/07
to greasemonkey-users
So, I want to insert a submitbutton to a form on a page, and I've got
that down. But what I want that button to do is run a function from
the userscript when the button is clicked.

How can I call a function from the userscript using onclick?

Regards,
Stian

Vectorspace

unread,
Oct 7, 2007, 6:36:15 AM10/7/07
to greasemon...@googlegroups.com
You can't, not with onlcick.

You need to use button.addEventListener() instead.

Example:

<input id="btn" value="Submit" type="button" />


button=document.getElementById("btn");
button.addEventListener("click", run, false);

function run()
{
do something

Anthony Lieuallen

unread,
Oct 7, 2007, 11:49:10 AM10/7/07
to greasemon...@googlegroups.com
On 10/7/2007 6:21 AM, Stian Hole wrote:
> How can I call a function from the userscript using onclick?

Read the wiki ( http://wiki.greasespot.net/Main_Page ).

Notice that the first external link is to the very thing you want (
http://www.oreillynet.com/lpt/a/6257 ).

Vectorspace

unread,
Nov 1, 2007, 11:38:50 AM11/1/07
to greasemon...@googlegroups.com
...sort of.

What I want to do is - if anything in the page is clicked apart from two
specific elements (and their children), do something.


Simple example DOM tree:

HTML
HEAD
BODY
#text
DIV 1
#text
TABLE
#text
DIV 2
DIV 3


I want to be able to run a function when an onclick event occurs on the
page, but not if it occurs on DIV's 2 or 3 or their children.

Right now I just assign an onclick event to everything else in BODY. Is
there a better way that that?

Thanks in advance

Anthony Lieuallen

unread,
Nov 1, 2007, 11:49:19 AM11/1/07
to greasemon...@googlegroups.com
On 11/1/2007 11:38 AM, Vectorspace wrote:
> I want to be able to run a function when an onclick event occurs on
> the page, but not if it occurs on DIV's 2 or 3 or their children.
>
> Right now I just assign an onclick event to everything else in BODY.
> Is there a better way that that?

Assign one click event to the top of the document (the body). Examine
the target of the event, to see if it is, or is inside, one of the two
elements you want to ignore or not. (Check the target, and walk up
.parentNode to check those.)

Vectorspace

unread,
Nov 5, 2007, 11:55:23 AM11/5/07
to greasemon...@googlegroups.com
Ok, I assign a click event to the document:
document.addEventListener("click",func,false)

How do I access the individual element that was clicked? I tried this:
document.addEventListener("click",function(){func(Event)},false)
to get the Event object but I can't find an attribute that indicates the
element the event occurred on (Event.target and Event.currentTarget both
return undefined)

Anthony Lieuallen

unread,
Nov 5, 2007, 12:11:12 PM11/5/07
to greasemon...@googlegroups.com
On 11/5/2007 11:55 AM, Vectorspace wrote:
>> Assign one click event to the top of the document (the body). Examine
>> the target of the event...

> Ok, I assign a click event to the document:
> document.addEventListener("click",func,false)
>
> How do I access the individual element that was clicked?...

Simplistic example:

<html>
<body>

<div>div</div>
<br />
<span>span</span>
<br />
<br />
<button>button</button>
<br />
<br />
<br />

<script type="text/javascript">
document.body.addEventListener('click', func, false);
function func(event) {
alert('you clicked the '+event.target.tagName);
}
</script>

</body>
</html>

Reply all
Reply to author
Forward
0 new messages