0

Hi anyone gives me some ideas to use the parameters as key in function?

for example, i am using vue, and have set html

<div id="app">
  <input ref="name" />
  <button @click="focusIt('name')">
    Click me
  </button>
</div>

and javascript code

new Vue({
    el: "#app",
  methods: {
    focusIt(value) {
        this.$refs.name.focus();
    }
  }
})

I have passed name as a parameter in the focusIt function, is there any way to use it inside the function as a key? something like this.$refs.name.focus()?

I have made a jsfiddle

Thanks for any advice!

Update:

I tried the this.$refs[value].focus(), but it does not work jsfiddle

James Chen
  • 833
  • 3
  • 10
  • 23

1 Answers1

0

If you want to pass down the name of the ref:

new Vue({
    el: "#app",
    methods: {
    focusIt(value) {
        this.$refs[value].focus();
    }
  }
})
Aydin4ik
  • 1,782
  • 1
  • 14
  • 19