...

View Full Version : how to create new event handlers



Kor
02-02-2004, 12:06 PM
It looks like setAttribute() can not create event handlers. Is there any other method to create new event handlers.

I need something like:

document.getElementById('id').setNewEvent('onclick','afunction()')

... while there is no initial such an event handler inside that DIV...

Any ideeas?

liorean
02-02-2004, 12:22 PM
[object HTMLElement].onevent=function(e){
e=e||window.event;
/* Insert your event handler code here.
The variable e is the event object.
The target element is e.target in W3C compliant browsers,
e.srcElement in ie. */
}Remember that event names should be lowercase.

Kor
02-02-2004, 12:39 PM
:confused: I don't get it... To stop the select scroll onmousewheel I need something like (obviously is aufully worng below)???:

<html>
<head>
<script>
document.getElementsByTagName('select')[0].onmousewheel = function(e){
e=e||window.event;
return false
}
</script>
</head>

<body>
<select>
<option>1</option>
<option>2</option>
<option>3</option>
</select>
</body>
</html>


(I need it this way, as I have many select objects on page (I'll build an increment for that, but till than I have to understand the method fo a single one...
?? :(

Garadon
02-02-2004, 04:38 PM
You can't access uninterpreted elements, so if you wan't to access an element by javascript u will need to either put the code in a function like I did thats called in the body's onload or put a code block behind the the element.



<html>
<head>
<script>
function block(){
document.getElementsByTagName('select')[0].onmousewheel = function(e){
e=e||window.event;
return false
}}
</script>
</head>

<body onload="block();">
<select>
<option>1</option>
<option>2</option>
<option>3</option>
</select>

</body>
</html>

Kor
02-03-2004, 09:19 AM
OK, thank you both, it works. I understood now this capture event method.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum