Zabs
Zabs

Reputation: 14142

Grab data attribute from an event

I have a jQuery event which is part of a backbone class that I am trying to grab the data-attribute from called 'data-foreground'

My function looks as follows..

foreground: function(e) {
    if (e.target.id === "") {
        this.findClickedElement(e, "li");
        return false;
    }

    console.log(e.target.id); // this returns the id which is '115'
    ... more code
}

How can I grab the data-attribute on the li as follows:

<li class="item" id="115" data-foreground="blue" />

Upvotes: 0

Views: 53

Answers (1)

Fabrizio Calderan
Fabrizio Calderan

Reputation: 123377

e.g. in vanillaJS using getAttribute()

e.target.getAttribute('data-foreground');

or with dataset, (accessing without data- prefix) (MDN)

e.target.dataset.foreground

Upvotes: 1

Related Questions