-1

i have this function in javascript :

document.addEventListener("click", function() {
    accSelect =document.getElementsByClassName("customSelectorListBox")[0].style.display;
    if(accSelect=="block" ) {
        document.getElementsByClassName("customSelectorListBox")[0].style.display="none";
    }
}, true);

How can i get the id of a div I click on, inside this function ?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
user2361682
  • 231
  • 1
  • 4
  • 7
  • possible duplicate of [get id of clicked element without putting any js code in the html](http://stackoverflow.com/questions/10270824/get-id-of-clicked-element-without-putting-any-js-code-in-the-html) – Andy Dec 05 '13 at 10:15

1 Answers1

3

Define the function to accept the event object [MDN] as argument:

document.addEventListener("click", function(event) {

Then you can get the clicked element via event.target [MDN] and access its ID:

event.target.id

I recommend to read all the excellent articles about event handling on quirksmode.org.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143