Need to get clicked elements 'Name' using Vue

Viewed 152

I am using Vue and I need to know which element was clicked as 1 is an i and the other is a button so I can pass a variable to a modal which is the same one for both as I am sharing the model but depending on which one was clicked depends on which text is displayed.

The easy (but wrong/long way) is to just have 2 click methods and pass the variable but I'd rather do it in 1 method call instead so I am no duplicating code but every time I get undefined back.

Current code

<i id="a" name="editAddress" class="fas fa-pencil-alt fa-lg px-4" v-tooltip:bottom="'Edit this address'" @click="editAddress"></i>

editAddress(e) {
  let curTarget = e.currentTarget;
  let acurTarget = e.currentTarget.id;
  let bcurTarget = e.currentTarget.name;
  console.log(curTarget)
  console.log(acurTarget)
  console.log(bcurTarget)

enter image description here

Tried code

<i id="a" name="editAddress" class="fas fa-pencil-alt fa-lg px-4" v-tooltip:bottom="'Edit this address'" type="submit" @click="handleClick($event)"></i>
<button id="addAddress" name="addAddress" class="btn btn-primary" @click.prevent="addAddress">Add a new address</button>

handleClick(e) {
  console.log('TARGET', e.target);
  console.log('NAME', e.target.name);
},

enter image description here

I can't figure out where I'm going wrong. I have also tried replacing currentTarget with target but still didn't work

1 Answers

You should use getAttribute method as follows :

console.log('NAME', e.target.getAttribute('name'));

new Vue({
  el: '#app',
  methods: {


    handleClick(e) {
      console.log('TARGET', e.target);
      console.log('NAME', e.target.getAttribute('name'));
    },
  }
})
<!DOCTYPE html>
<html>

<head>
  <meta name="description" content="Vue.delete">
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.2.1/vue.min.js"></script>
</head>

<body>
  <div id="app">
    <i id="a" name="editAddress" class="fas fa-pencil-alt fa-lg px-4" v-tooltip:bottom="'Edit this address'" type="submit" @click="handleClick($event)">a</i>

  </div>

Related