Solved

Detect when element is clicked and grab ID if present

Posted on 2014-11-17
5
123 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 500 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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
In this tutorial viewers will learn how to style rounded corners for elements in CSS using the border-radius property Begin with a normal styled element such as a div: To style all four corners of the div to be the same degree of roundness, use the …
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…

717 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