FloHaberfellner
FloHaberfellner

Reputation: 37

How to get id from HTML Element in Vue.js

I have a Vue application, where I have several identic fontawesome logos with different id's.

<p @click="getId($event)" class="trash"><i id="1"  class="fas fa-trash-alt"></i></p>
<p @click="getId($event)" class="trash"><i id="2"  class="fas fa-trash-alt"></i></p>
<p @click="getId($event)" class="trash"><i id="3"  class="fas fa-trash-alt"></i></p>

Now when I click on one of those logos i execute the following method:

getId(event){
      console.log(event.currentTarget.id);
    }

When I now click on a logo i get an empty console output. I googled this problem many times and also tried things like:

<p class="trash"><i id="1" @click="getId(this)"  class="fas fa-trash-alt"></i></p>

getId(logo){
  console.log(logo.id);
}

In this case I return a undefined. What could be the problem?

Upvotes: 1

Views: 3256

Answers (1)

kamran hassan
kamran hassan

Reputation: 166

<p class="trash"><i id="1" @click="getId(this)"  class="fas fa-trash-alt"></i></p>

getId(logo){
 console.log(event.target.id);
}

as Deepak mentioned in comment all you need to do is get event target id to get value from elemen.

Upvotes: 1

Related Questions