2

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(',');

0

2 Answers 2

1

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)
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

Yes it did helppp.. just try now. Thank you very muchhhh!!!!
Glad to hear. Enjoy Vue!

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.