2
votes

i want to get the value of the button when click i already add a method. but whenever i click theres nothing. the value not passing. can anyone help me? Thank you.

Here is the code:

<button
class="button buttonvalue"
v-for="(p,index) in buttonvalue"
:key="index"
@click="webcamSendRequestButton($event)"
>{{p}}</button>

Method:

  methods: {
    webcamSendRequestButton: function(e) {
    // const buttonValue = e.target.value;
    // console.log(e.target.value)
    alert(e.target.value);
    }
  }

DATA (Json) of button value

array data: small,medium,large

this.buttonvalue= this.item[0].buttonvalue.split(',');

2

2 Answers

1
votes

To get value button value you can either bind a :value attribute first like:

<button
  class="button buttonvalue"
  v-for="(p,index) in buttonvalue"
  :key="index"
  @click="webcamSendRequestButton($event)"
  :value="p"
>{{p}}</button>

Then e.target.value will work fine.


Or, if you don't want to add another attribute, then you can simply use:

methods: {
  webcamSendRequestButton(e) {
     console.log(e.target.textContent)
  }
}

instead of using e.target.value.


Or, if the value p is required, then you can simply pass that to click handler like:

@click="webcamSendRequestButton(p)"

and then access it like:

methods: {
  webcamSendRequestButton(buttonvalue) {
     console.log(buttonvalue)
  }
}
1
votes

If you simply need to pass a value of p, for example then you would write:

@click="webcamSendRequestButton(p)"

then in your method:

  methods: {
    webcamSendRequestButton(val) {
       alert(val);
    }
  }

I am assuming that buttonvalue is some kind of array that you are looping through.

Hope it helps.