Peter
Peter

Reputation: 11835

Button onclick event (hard coded)


is it possible to check the e.button "directly" in the onclick tag?

For example

<div onclick="if(e.button == 1) { alert(e.button); }"></div>

Thanks in advance!
Peter

Upvotes: 0

Views: 4328

Answers (2)

Felix Kling
Felix Kling

Reputation: 816452

The content you put into onclick will be wrapped in a function so theoretically you can put any JavaScript there:

<div onclick="var e = event || window.event; if(e.button == 1) { alert(e.button); }"></div>

But it is not considered to be good practice as it is hard to maintain and mixes HTML and JavaScript. Better is to attach the function with JavaScript:

// given your HTML is
<div id="myDIV"></div>

// you can do:
document.getElementById('myDIV').onclick = function(event) {
   var e = event || window.event; 
   if(e.button == 1) { 
       alert(e.button); 
   }
}

quirksmode.org provides every good information about JavaScript and events.

Upvotes: 2

user703016
user703016

Reputation: 37945

Yes. It looks like :

<div onclick="javascript:var e = window.event; if(e.button == 1) { alert(e.button); }"></div>

Upvotes: 3

Related Questions