473,396 Members | 1,966 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,396 software developers and data experts.

Event Detection of elements at runtime

Hi
Is it possible to detect event of an element on runtime without
explicitly attaching event by using onClick() etc?.

I have a page which can generate run time DIVs and form elements and I
don't even have their names or Ids for reference. Is it possible in
Javascript to capture which element was clicked and then can refer
that element by returning it's ID or tag type?
Thanks

Aug 22 '07 #1
2 1428
Adnan Siddiqi wrote:
Is it possible to detect event of an element on runtime without
explicitly attaching event by using onClick() etc?.

I have a page which can generate run time DIVs and form elements and I
don't even have their names or Ids for reference. Is it possible in
Javascript to capture which element was clicked and then can refer
that element by returning it's ID or tag type?
Yes, through event bubbling[1]. The event has to bubble (`click' does in
the standard DOM and all known proprietary DOMs), and one parent element has
to have an event listener attached for the event. For example:

....
<head>
<meta http-equiv="Content-Script-Type" content="text/javascript"></meta>
<script type="text/css">
// <![CDATA[
function clickHandler(e)
{
if (e)
{
var t = e.target || e.srcElement;
if (t) window.alert(t.id);
}
}
// ]]>
</script>
</head>

<body onclick="clickHandler(event)">
...
<div id="foo">
...
</div>
...
</body>
....

As you can see, once you have the element object reference (t), you don't
need the ID or its element type ("tag name") anymore to refer to it.
HTH

PointedEars
___________
[1] http://www.w3.org/TR/DOM-Level-2-Eve...-flow-bubbling
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Aug 22 '07 #2
Thomas 'PointedEars' Lahn wrote:
<script type="text/css">
<script type="text/javascript">
Aug 23 '07 #3

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

Similar topics

4
by: Eric | last post by:
How can I dynamically assign an event to an element? I have tried : (myelement is a text input) document.getElementById('myelement').onKeyUp = "myfnc(param1,param2,param3)"; ...
21
by: news.btinternnet.com | last post by:
I can do this in IE myLink.click(); //Invoking the handler as if the user had clicked on the link themselves. I need to be able to do the same in Netscape Navigator, can anyone help ?
6
by: Tom | last post by:
Hi, In the following code I have reproduced a problem in IE that is extremely annoying for my application. <html> <head> <script type="text/javascript">
1
by: Rohit Raghuwanshi | last post by:
Hello all, we are running a delphi application with DB2 V8.01 which is causing deadlocks when rows are being inserted into a table. Attaching the Event Monitor Log (DEADLOCKS WITH DETAILS) here....
10
by: Sean Dockery | last post by:
I have the following HTML file that I've been using for testing... <html> <head> <script type="text/javascript"> <!-- function handleWindowLoad() { var items = ; for (var i = 0; i < 11; i++)...
10
by: fusillo | last post by:
Hi, i've tried a test code about the Event Listener API but i've some problem about detaching the element firing the event with IE. Here's my code: <html> <head> <style type="text/css">...
8
by: kk_oop | last post by:
Hi. Any recommendations for memory leak detection tools for C++ running on Linux or Solaris? I'm interested in static (compile time) and runtime detection. It's for a large project. Thanks! ...
3
by: alvinwoon | last post by:
Hi guys, I find it frustrating that I need to sniff out IE to make event handler for the form's select element work across different browsers. Here's a summary of what I have right now: var...
15
by: RobG | last post by:
When using createEvent, an eventType parameter must be provided as an argument. This can be one of those specified in DOM 2 or 3 Events, or it might be a proprietary eventType. My problem is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.