0

I want to use the Id of the element in which I use the function as a parameter. Example:

<div
    class="col-2 column-center"              
    id="myId"              
    @mouseover="aFunction(id)"              
>

methods: {    
    aFunction(id){
      alert(id);
    }
 }

But it doesn't work. How can I do it?

0

2 Answers 2

6

In another way, you can make your id attribute bind with the data property, like this :

<template>
  <div class="col-2 column-center" v-bind:id="id" @mouseover="aFunction(id)">
    test
  </div>
</template>

<script>
export default {
  name: 'Test',
  data() {
    return {
      id: 'myId',
    };
  },
  methods: {
    aFunction(id) {
      alert(id);
    },
  },
};
</script>
Sign up to request clarification or add additional context in comments.

Comments

2

You can pass the event to the method and then get the id from event.target.id.

https://jsfiddle.net/tr0f2jpw/

new Vue({
  el: '#app',
  data: {
    message: 'Hello Vue.js!'
  },
  methods: {    
    aFunction(event){
      alert(event.target.id);
    }
 }
})
<script src="https://unpkg.com/vue"></script>

<div id="app">
  <div
    class="col-2 column-center"              
    id="myId"              
    @mouseover="aFunction($event)"              
  >
  some stuff
  </div>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.