How can I call a function from the userscript using onclick?
Regards,
Stian
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
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 ).
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
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.)
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)
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>