?
Solved

Detect when element is clicked and grab ID if present

Posted on 2014-11-17
5
Medium Priority
?
132 Views
Last Modified: 2014-11-19
Is there a way to listen if an element of a page is clicked and then grab the ID of that element if it exists? No JQuery please, ONLY Javascript.
0
Comment
Question by:894359
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 22

Expert Comment

by:Mrunal
ID: 40449445
hi,
Here is your solution.
0
 
LVL 22

Accepted Solution

by:
Mrunal earned 2000 total points
ID: 40449447
HTML code:

<div id="Div1">
    DIV 1 CONTENT</div>
<span>
    DIV 2 CONTENT no id</span>
<div id="Div3">
    DIV 3 CONTENT</div>

Open in new window


JavaScript Code:

document.addEventListener('click', function(e) {
    e = e || window.event;
    var target = e.target || e.srcElement;
    if(target.id != "")
    {
        alert(target.id);
    }
    //var text = target.textContent || text.innerText;    
}, false);

Open in new window


CSS code:

div, span{
    padding: 10px;
    background-color: #CCC;
    margin: 5px;
    border: solid 2px #888;
    display: block;
}

Open in new window

0
 
LVL 4

Expert Comment

by:ItWorked
ID: 40449589
Is this what you want?
It's on fiddel

Let me know if it's not what you were looking for!
0
 
LVL 22

Expert Comment

by:Mrunal
ID: 40449735
Hi ItWorked,
Thanks for sharing your way of solution.
If we search all elements from document and then loop them and fire onclick of each of the element, it will not good for performance.
Also it will loop through all for matching particular element.
And,
if we add EventListener to document, it will be having better performance.

Also if we need to get the parent element's id then we can refer below solution:

Next Solution
0
 
LVL 4

Expert Comment

by:ItWorked
ID: 40451804
@Mrunal,
Yes you are right! And thanks for sharing your view.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
The viewer will learn how to dynamically set the form action using jQuery.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question