2

.html:

<div id="app">    
 <input type="hidden" name="data" value="Hello" id="data">
</div>

script:

new Vue({
  el: "#app",
  data() {
    return {
      someData: "",
    };
  },
});

I want to pass value(Hello) to someData I've tried v-model but it don't work with hidden inputs

3
  • 1
    where is the v-model Commented Sep 8, 2020 at 5:41
  • i removed it from code because it didn't help. Can you give me proper code example to bind the data to hidden input? Commented Sep 8, 2020 at 5:43
  • it should work, also did you check console for errors Commented Sep 8, 2020 at 5:50

1 Answer 1

6

Assuming you're generating that HTML server-side and want to get the value from HTML into Vue, you can simply add a ref to the <input> and in the mounted hook, read the value

new Vue({
  el: "#app",
  data: () => ({
    someData: ""
  }),
  mounted () {
    // read the "value" from the input element
    this.someData = this.$refs.data.value
  }
})
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.min.js"></script>

<div id="app">
  <!--                👇 note the new "ref" attribute -->
  <input type="hidden" ref="data" name="data" value="Hello" id="data">

  <!-- just an example to show the data has been assigned -->
  <pre>someData = {{ someData }}</pre>
</div>


If you don't want to add ref or any other changes to the HTML, you can of course query the DOM directly

mounted () {
  this.someData = document.getElementById("data").value
}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks it works, but it is possible without ref? i mean some kind of "v-" vue things. And sorry i can give thumbs up because i'm less than 15 reputation
@Farid I've added an example without ref

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.