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

shahr's avatar
Level 10

How to change jQuery to Vue v2.6.14?

I have an element id of showType.

<div id="showType"

</div>

And I want to change these codes to vue.

$.ajax({
    url: '{{ route('product.warranty.save') }}',
    type: 'POST',
    data: '',
    success: function (data) {
        $('#showType').html(data);
    }
});
0 likes
1 reply
LaryAI's avatar
Level 58

To change this code to Vue, you can use the axios library to make the AJAX request and update the showType element using Vue's reactivity system.

First, add the axios library to your project by including it in your HTML file:

<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>

Then, create a new Vue instance and define a data property for the showType element:

<div id="app">
  <div v-html="showType"></div>
</div>

<script>
new Vue({
  el: '#app',
  data: {
    showType: ''
  }
});
</script>

Next, update the AJAX request to use axios and update the showType property when the request is successful:

<script>
new Vue({
  el: '#app',
  data: {
    showType: ''
  },
  mounted() {
    axios.post('{{ route('product.warranty.save') }}')
      .then(response => {
        this.showType = response.data;
      });
  }
});
</script>

This code will make the AJAX request when the Vue instance is mounted, and update the showType property with the response data. The v-html directive is used to render the HTML content of the showType property in the template.

Note: Make sure to replace {{ route('product.warranty.save') }} with the actual URL for your AJAX endpoint.

Please or to participate in this conversation.