Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: get element ID from event

How to get the ID of an element passed as (e)?

window.addEventListener('load', function(){

    var tags = document.getElementsByClassName("tag");
    for (i=0; i<tags.length; i++){
    tags[i].addEventListener('mousedown', function(e){ tagClick(e) }, false);
    }

}, false);

function tagClick(e){

    /* here I'm gonna need the event to cancel the bubble and the ID to work with it*/

    alert('The id of the element you clicked: ' + [?object].id);

    [?object].className='newClass';

    e.stopPropagation();
    e.cancelBubble = true;
}

I need to get the element/object inside tagClick so I can change its properties

html:

<div class="tag">
    <img src="/images/tags/sample.jpg"/>
    <label class="tagLabel">Sample</label>
</div>

See, the element with the event attached is the div, but ig gives me the image object instead when using e.srcElement.

like image 853
Azevedo Avatar asked Jan 01 '14 13:01

Azevedo


People also ask

How do I find the name of an event in jQuery?

Answer: Use the event.target Property You can use the event.target property to get the ID of the element that fired an event in jQuery. This property always refers to the element that triggered the event. The following example will display the name of the element that was just clicked.

How to get element by id in JavaScript?

Another way Javascript get element by id is by using the querySelector method. It is a method to select the element in JavaScript using CSS selectors. It returns the first element within the document that matches the given selector.

How to get the ID of the element that fired an event?

You can use the event.target property to get the ID of the element that fired an event in jQuery. This property always refers to the element that triggered the event. The following example will display the name of the element that was just clicked.

How to get the ID of the element that was clicked on?

If that can't be done, is there another way to get the id of the element that was clicked on? You can use e.target.id. e.target represents DOM object and you can access all its property and methods. $ ('body').on ('click', function (e) { alert (e.target.id); });


2 Answers

When you bind an event listener with addEventListener, it's called with this referring to the element you bound the event on. So this.id will be the id of the element (if it has one).

alert('The id of the element you clicked: ' + this.id);

But you're breaking that with this line:

tags[i].addEventListener('mousedown', function(e){ tagClick(e) }, false);

...because you're putting an extra function in the middle, then calling tagClick without setting this. There's no need for that extra function, change that to:

tags[i].addEventListener('mousedown', tagClick, false);

...so this doesn't get messed up. Or alternately if you prefer to have the extra function, ensure this is maintained using Function#call:

tags[i].addEventListener('mousedown', function(e){ tagClick.call(this, e) }, false);

...but there's no reason to do that with the tagClick function shown.

The (standard) event object also has the properties target (which may not be the element you bound the event on, it may well be a descendant) and currentTarget (which will be the element you bound the event on). But this is convenient and reliable if you use addEventListener (or even attachEvent, on IE).

like image 192
T.J. Crowder Avatar answered Sep 21 '22 09:09

T.J. Crowder


You can get the target of the event with e.target.

However keep in mind that some browsers consider text nodes to be a target, so try something like this:

var t = e.target;
while(t && !t.id) t = t.parentNode;
if( t) {
    alert("You clicked element #"+t.id);
}

This will find the first element that actually has an ID.

Happy New Year!

EDIT: On second thought, if it's the "tag" element itself you want to refer to, just use this. In an event handler, this refers to the element that actually has the handler. Although in this case you'll need to change your handler to ('mousedown', tagClick, false)

Or better still:

document.body.addEventListener("mousedown",function(e) {
    var t = e.target;
    while(t && t.nodeName != "TAG") { // note, must be uppercase
        t = t.parentNode;
    }
    if( t) {
        alert("You clicked on #"+t.id);
    }
},false);

Fewer event handlers is always better.

like image 38
Niet the Dark Absol Avatar answered Sep 24 '22 09:09

Niet the Dark Absol