Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
42
rated 0 times [  43] [ 1]  / answers: 1 / hits: 16232  / 13 Years ago, mon, february 20, 2012, 12:00:00

First of all, I am not looking for jQuery solution, just simple pure Javascript code, inside of an element.


Let's say we have following html code:


<select onmousedown=" ??? ">...</select>

I want a simple script inside of the element to show popup message alert() with information which button was pushed down and what is a relative position of the element to the document <body> - something like offset() in jQuery.


More From » html

 Answers
74

Create a JavaScript function with some name and then call it on onmousedown event passing the event and this object which can be used inside the function.



HTML



<select onmousedown=onMouseDown(event, this)>...</select>


JS



function onMouseDown(e, obj){
e = e || window.event; //window.event for IE

alert(Keycode of key pressed: + (e.keyCode || e.which));
alert(Offset-X = + obj.offsetLeft);
alert(Offset-Y = + obj.offsetTop);

}


If you plan to use jQuery then you can use this script



$('select').mousedown(function(e){
alert(Keycode of key pressed: + e.which);

//Inside the handler this points to the select DOM element
alert(Offset-X = + $(this).offset().left);
alert(Offset-Y = + $(this).offset().top);
});


Update:



If you want inline script then try this.



<select onmousedown=function(e, obj){ e = e || window.event;alert('Keycode of key pressed: ' + (e.keyCode || e.which));alert('Offset-X = ' + obj.offsetLeft);alert('Offset-Y = ' + obj.offsetTop);}(event, this);>...</select>

[#87344] Saturday, February 18, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jazmyne

Total Points: 503
Total Questions: 102
Total Answers: 99

Location: Svalbard and Jan Mayen
Member since Sun, Sep 25, 2022
2 Years ago
;