473,394 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

can you add whole types of elements to addEventListener? if not, how else to achieve the efffect?

37 Signals has built some awesome software with some features I wish I
knew how to imitate. When I'm logged into my page
(http://lkrubner.backpackit.com/pub/337271) any item that I mouseOver
I'm offered the chance to delete or edit it.

I'm trying to figure out how that is done. It seems like either you can
all LI elements to an addEventListener kind of thing, or you do
something like this:

document.onmouseover= moveDeleteControls;

And then the function moveDeleteControls magically figures out the id
of the element being moused over, yes?

Anyone know which approach is possible?

Feb 6 '06 #1
3 1990
OK, I figured it out. This is cool stuff. I did this:

document.onmouseover= moveDeleteControls;

and now as I move the mouseover the page, this shows me what element
the mouse is over:

function moveDeleteControls(e) {
var eventTargetRef = e.target;
var newId = eventTargetRef.id;
alert(newId);
}

Awesome. Easy enough to take out the alert() and do something useful
with that. Does anyone know if this is cross-browser?


Jake Barnes wrote:
37 Signals has built some awesome software with some features I wish I
knew how to imitate. When I'm logged into my page
(http://lkrubner.backpackit.com/pub/337271) any item that I mouseOver
I'm offered the chance to delete or edit it.

I'm trying to figure out how that is done. It seems like either you can
all LI elements to an addEventListener kind of thing, or you do
something like this:

document.onmouseover= moveDeleteControls;

And then the function moveDeleteControls magically figures out the id
of the element being moused over, yes?

Anyone know which approach is possible?


Feb 6 '06 #2
Jake Barnes wrote:
OK, I figured it out. This is cool stuff. I did this:

document.onmouseover= moveDeleteControls;

and now as I move the mouseover the page, this shows me what element
the mouse is over:

function moveDeleteControls(e) {
var eventTargetRef = e.target;
var newId = eventTargetRef.id;
alert(newId);
}

Awesome. Easy enough to take out the alert() and do something useful
with that. Does anyone know if this is cross-browser?


No, it's not - try it in IE. The following is:
function moveDeleteControls(e)
{
var e = e || window.event;
var eventTargetRef = e.target || e.srcElement;
document.getElementById('fred').innerHTML = eventTargetRef.nodeName
+ ((eventTargetRef.id)? ' ' + eventTargetRef.id:'');
}
Provided you have a div or span somewhere with an id of 'fred'.
Read about the different event models here (this is the first of several
pages, work through them all):

<URL:http://www.quirksmode.org/js/introevents.html>

--
Rob
Feb 7 '06 #3
Jake Barnes wrote:
OK, I figured it out. This is cool stuff. I did this:

document.onmouseover= moveDeleteControls;

and now as I move the mouseover the page, this shows me what element
the mouse is over:

function moveDeleteControls(e) {
var eventTargetRef = e.target;
var newId = eventTargetRef.id;
alert(newId);
}

Awesome.
Eeek. It most definitely is not.
Easy enough to take out the alert() and do something useful
with that. Does anyone know if this is cross-browser?
It is not. For two reasons:

1. `onmouseover' is a proprietary property of the object referred to using
the proprietary `document' reference (where for the latter there is alas
no standards-compliant alternative). A browser (or any HTML user agent
for that matter) may or may not implement it, and it may or may not
implement it as if an assignment of a reference to a Function object
was considered the addition of an event listener in combination with
the removal of all other event listeners.

2. Passing an Event object reference to an event listener and exposing a
`target' property for this object referring the EventTarget object that
caused the event is an implementation of W3C DOM Level 2 Event's
EventListener, Event and EventTarget interfaces. These interfaces are
not implemented in the IE DOM (properly) (yet; hopefully IE7 will).
However, these interfaces apply only to document nodes, and the object
referred to with `document' refers to no such object. And the
`mouseover' event which is addressed by this approach "is valid for most
_elements_" only, where the object referred to by `document' usually
does not refer to an element object (but a HTMLDocument object).
Therefore, a UA may or may not implement as if either one did, and ISTM
it was standards compliant if it did not implement either.

That said, here is the (almost, see above) standards-compliant approach
that also takes IE's incomplete event DOM implementation into account
(watch for word wrap):

<head>
...
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
/**
* @author
* (C) 2003, 2004 Thomas Lahn &lt;ty******@PointedEars.de&gt;
* Distributed under the GNU GPL v2.
* @partof
* http://pointedears.de/scripts/types.js
* @optional string s
* String to be determined a method type, i.e. "object"
* in IE, "function" otherwise. The type must have been
* retrieved with the `typeof' operator, thus this method
* is applicable to unknown properties while
* @link{#isMethod()} is not. Note that this method
* may also return <code>true</code> if the value of
* the <code>typeof</code> operand is <code>null</code>; to
* be sure that the operand is a method reference, you have
* to && (AND)-combine the <code>isMethodType(...)</code>
* expression with the method reference identifier.
* @return type boolean
* <code>true</code> if <code>s</code> is a method type,
* <code>false</code> otherwise.
* @see #isMethod()
*/
function isMethodType(s)
{
return (s == "function" || s == "object");
}

/**
* Adds an event-handling function (event listener) for a
* DOM object as event target. The following methods are
* used (in order of preference):
*
* - addEventListener(...) method (W3C-DOM Level 2)
* - attachEvent(...) method (proprietary to MSIE 5+)
* - Assignment to event-handling property (MSIE 4+ and others)
*
* @author
* (C) 2004-2006 Thomas Lahn &lt;dh******@PointedEars.de&gt;
* @partof
* http://pointedears.de/scripts/dhtml.js
* @param o : DOMObject
* Reference to the DOM object.
* @param sEvent : string
* Required string to be used as event identifier.
* If the addEventListener(...) method is not available,
* `on' is used as its prefix to reference the respective
* proprietary event-handling property.
* @param fListener : Function
* Reference to the Function object that provides
* event-handling code. Use <code>null</code> to
* remove the event handler if, and only if, the
* proprietary event-handling property is available.
* @param bUseCapture : optional boolean
* Optional. If <code>true</code>, the argument indicates that
* the user wishes to initiate capture. Corresponds to the
* third parameter of the addEventListener(...) method, is
* ignored if that method is not supported by the DOM (object).
* @return type boolean
* <code>true</code> on success, <code>false</code> otherwise.
* Since addEventListener(...) returns no value and throws
* no exceptions (what a bad design!), it is considered to be
* successful always, while attachEvent(...) returns success
* or failure, and the new value of the proprietary
* event-handling property must match the assigned value for
* the method to be successful.
* @see
* dom2-events#Events-EventTarget-addEventListener,
* msdn#workshop/author/dhtml/reference/methods/attachevent.asp,
*/
function _addEventListener(o, sEvent, fListener, bUseCapture)
{
var result = false;

if (o && sEvent && isMethodType(typeof fListener))
{
if (isMethodType(typeof o.addEventListener))
{
o.addEventListener(sEvent, fListener, !!bUseCapture);
result = true;
}
else
{
if (isMethodType(typeof o.attachEvent))
{
result = o.attachEvent("on" + sEvent, fListener);
}

if (!result)
{
o["on" + sEvent] = fListener;
result = (o["on" + sEvent] == fListener);
}
}
}

return result;
}

function moveDeleteControls(e)
{
if (!e && window.event)
{
e = window.event;
}

if (e)
{
var eventTargetRef = e.target || e.srcElement;
if (eventTargetRef)
{
newId = eventTargetRef.id;
// ...
}
}
}
</script>
</head>

<body onload="_addEventListener(document.body, 'mousemove',
moveDeleteControls, false);">
...
</body>
[top posting again]


Learn to quote. NOW.

<URL:http://jibbering.com/faq/faq_notes/pots1.html#ps1Post>
PointedEars
Feb 7 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: abs | last post by:
Hi all. My list: <ul> <li id="a" onclick="show(this)">Aaaaaaaa</li> <li id="b" onclick="show(this)">Bbbbbbbb</li> <li id="c" onclick="show(this)">Cccccccc <ul> <li id="d"...
4
by: PJ | last post by:
Is it possible to extend the Node object so that the DOM function addEventListener can be used w/ IE? Does anyone have an example of this? Thanks, Paul
4
by: VK | last post by:
I have this code failing to work in IE: getElementsByTagName doesn't return elements from my JS namespace. What's wrong? <html xmlns:js> <head> <title>Que</title> <meta...
23
by: digitalorganics | last post by:
How can an object replace itself using its own method? See the following code: class Mixin: def mixin(object, *classes): NewClass = type('Mixin', (object.__class__,) + classes, {}) newobj =...
13
by: anil.rita | last post by:
When the user chooses an AV file to play, based upon the type of file, I want to use the default installed media player to play it. I am wondering if this is a good way - any alternatives,...
7
by: mavigozler | last post by:
IE7 does not appear to set an event on contained text inside SPAN elements whose 'onclick', 'onmouseover', and 'onmouseout' events, defying the HTML recommendation. Firefox appears to conform. ...
5
by: debrief | last post by:
Hello, I have been trying to hide objects on the onclick event and it works fine in Firefox however IE does not respond at all. Is there a way around this? I have the following in my...
1
by: kidelectric | last post by:
The issue I am having is that I would like to be able to drag-and-drop div elements that have rounded corners.* Since these elements will be dynamically created (including background color), I could...
0
by: wpjoju | last post by:
i have this code which adds an event listener to a newly opened window but it doesn't seem to work in google chrome. the problem is window.addEventListener seem to work in chrome but if you do...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.