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

laravel_ninja's avatar

Get value from name attribute in vue input

Hi I'm new to vue js I just want to ask can I get value of input form by its name in vue js without data binding

0 likes
2 replies
LaryAI's avatar
Level 58

Yes, you can get the value of an input form by its name attribute in Vue.js without data binding. You can use the ref attribute to reference the input element and then access its value using the $refs property.

Here's an example:

<template>
  <div>
    <input type="text" name="myInput" ref="myInput">
    <button @click="getValue">Get Value</button>
  </div>
</template>

<script>
export default {
  methods: {
    getValue() {
      const value = this.$refs.myInput.value;
      console.log(value);
    }
  }
}
</script>

In this example, we have an input element with the name "myInput" and a button that triggers the getValue method. Inside the getValue method, we access the input element using this.$refs.myInput and then get its value using the value property. Finally, we log the value to the console.

Note that using ref is not recommended in most cases, as it goes against the reactive nature of Vue.js. It's usually better to use data binding to keep your component state in sync with the input values.

Ben Taylor's avatar

It's just JavaScript at the end of the day, so yes you can. But if you are going to use Vue (or any other framework), you should really try to learn to do things the framework way, otherwise why bother?

1 like

Please or to participate in this conversation.