Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MDN's avatar
Level 1

Getting the value of a button using Vue.js

I have a form that has three buttons and each need a value of 1, 2 and 3 respectively. They're placed in a form and the user is to click one of the three resulting in the form changing. I'm attempting to make an ajax call based upon the value of the button the user has pressed. I'm attempting this like so:

<form method="POST">
<input type="hidden" name="_token" id="token" value="{{ csrf_token() }}">

 <div class="col-md-4 col-sm-6 feature text-center">
    <div class="content">
         <button class="btn btn-default btn-question" 
         v-model="selectedcategory"
          v-on="click: search" 
         value="1">Category 1
         </button>
     </div>
</div>

 <div class="col-md-4 col-sm-6 feature text-center">
    <div class="feature-content">
      <button class="btn btn-default btn-question" 
      v-model="selectedcategory" 
      v-on="click: search" 
       value="2">Category 2
      </button>
     </div>
</div>

<div class="col-md-4 col-sm-6 feature text-center">
     <div class="feature-content">
      <button class="btn btn-default btn-question" 
       v-model="selectedcategory" 
      v-on="click: search" 
      value="3">Category 3
         </button>
     </div>
</div>
</form>



Vue.http.headers.common['X-CSRF-TOKEN'] = document.querySelector('#token').getAttribute('value');

new Vue({
 el: '#picker',

    data: {
        selectedcategory: '0'
  },

  methods:{

      search: function(e){
         e.preventDefault();

         var getresults = this.$http.get('api/products/' + this.selectedcategory, function(products){
              this.$set('products', products); //key, data
         });
     }
    }
});

Like this, I'm unable to get the value of the buttons but if I do a simple select drop down menu like so

<select v-model="selectedcategory" v-on="click: 
 <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
</select>
0 likes
6 replies
MDN's avatar
Level 1

Should that be on the form element itself or the button?

jekinney's avatar
Level 47

your not passing the value in to vue. Instead of v-model just use your v-on="click: search(1) etc. no need for a value. You can also do away with the form and use type="button" then you don't need preventDefault().

1 like
veve286's avatar

EZ trick @MDN

 <div class="col-md-4 col-sm-6 feature text-center">
    <div class="content">
         <button class="btn btn-default btn-question" 
         v-model="selectedcategory"
          v-on="click: search(1,$event)" 
         value="1">Category 1
         </button>
     </div>
</div>

 <div class="col-md-4 col-sm-6 feature text-center">
    <div class="feature-content">
      <button class="btn btn-default btn-question" 
      v-model="selectedcategory" 
      v-on="click: search(2,$event)" 
       value="2">Category 2
      </button>
     </div>
</div>

      search: function(btn,e){
         e.preventDefault();

    if(btn==1){
        alert('press 1);
    }
    if(btn==2){
        alert('press 2);
    }
       
     }
1 like
MDN's avatar
Level 1

Thanks @jekinney and @veve286 !!

Managed to get it to work with your help. Had feeling I didn't need the form. JS is definitely my weakest point. Thanks again guys.

jekinney's avatar

@MDN I know it's not "right" in html not to use form tags but with JavaScript in can be a pain!!!

Glad you got it working.

Please or to participate in this conversation.