Solved

Detect when element is clicked and grab ID if present

Posted on 2014-11-17
5
98 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
  • 3
  • 2
5 Comments
 
LVL 22

Expert Comment

by:Mrunal
Comment Utility
hi,
Here is your solution.
0
 
LVL 22

Accepted Solution

by:
Mrunal earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
@Mrunal,
Yes you are right! And thanks for sharing your view.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to count occurrences of each item in an array.
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now