0

i need the id of the div that is pressed, my code looks like this right now, how do i get it?

window.onload = function() {
    document.getElementById("one").onclick = call;
    document.getElementById("two).onclick = call;
}

function call () {
     var id = I need the id here.....
}
Sachin
  • 901
  • 2
  • 10
  • 23
  • Possible duplicate of [Finding the id of a parent div using Jquery](http://stackoverflow.com/questions/545978/finding-the-id-of-a-parent-div-using-jquery) – Satej S Feb 25 '16 at 03:14
  • Possible duplicate of [Getting the ID of the element that fired an event](http://stackoverflow.com/questions/48239/getting-the-id-of-the-element-that-fired-an-event) – Frank Bryce Feb 25 '16 at 03:36

1 Answers1

0

The call function will be invoked with the event object as an argument. event.target will return the original element on which the event occurred. You can get the ID of that element using event.target.id.

Also, I don't think your code works because there's a missing quote on your second document.getElementById.

function call(event) {
    var id = event.target.id;
}
linstantnoodles
  • 4,350
  • 1
  • 22
  • 24