N
N
Nik17122017-04-21 13:34:11
Vue.js
Nik1712, 2017-04-21 13:34:11

How to pass attribute to vue method?

There is a script

var pathArray = location.href.split( '/' );
var protocol = pathArray[0];
var host = pathArray[2];
var url = protocol + '//' + host;

var itemlist = new Vue({
    el: '#itemlist',
    data: {
        json: null
    },
     methods: {
      getVariant: function (event) {
        var good = this.getAttribute('data-id');
        $.getJSON(url + good, function (json) {
          itemlist.json = json;
          console.log(itemlist.json);
      });
    }
  }
});

and html
<li class="color-variant__choose color-variant__choose_<?=$value?>" 
    data-id="12345" //к примеру, но вообще она может быть любой 
    v-on:click="getVariant"
>
</li>

throws an error
vue_app.js:72 Uncaught TypeError: this.getAttribute is not a function
how to pass the value of the data-id attribute so that there is no error?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Kulakov, 2017-04-21
@Nik1712

To access the DOM object in an event: event.target.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question