the key to unlocking the potential of a function lies in utilizing parameters

Can someone suggest ways to utilize parameters as keys in a function?

For example, I am using Vue and have the following HTML setup:

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

Accompanied by this JavaScript code:

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

Is there a way to use the parameter passed in the focusIt function as a key inside the function like this.$refs.name.focus()?

I created a jsfiddle for reference.

Thank you for any guidance!

Update:

I attempted to use this.$refs[value].focus(), but it did not work. Here is the updated jsfiddle

Answer №1

To keep the reference name:

new Vue({
    el: "#app",
    methods: {
    focusInput(inputName) {
        this.$refs[inputName].focus();
    }
  }
})

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Populating an item in a for each loop - JavaScript

In the following example, I am struggling to populate the object with the actual data that I have. var dataset = {}; $.each(data.body, function( index, value ) { dataset[index] = { label: 'hello', ...

What is the best way to style radio boxes in HTML to resemble checkboxes and display X's when selected?

I'm looking to create a form with radio boxes that resemble checkboxes and display a glyphicon x when selected. I've experimented with various solutions such as: input[type="radio"] { -webkit-appearance: checkbox; /* Chrome, ...

Using Three.js to display a PerspectiveCamera with a visible bounding Sphere

My challenge is to create a Scene that loads a single .obj file where the object is fully visible in the PerspectiveCamera upon scene initialization. The field of view (FOV) is set at 60 The objects vary in size TrackballControls are utilized for camera ...

React component is unable to identify prop

I'm attempting to send an array of objects from the main App.js file to a smaller component using props. However, for some reason, the prop is not being recognized within the smaller component file. https://i.stack.imgur.com/WuyFr.png https://i.stac ...

Unable to display a recursive component with Vue3 CDN

I am currently working on a project using Vue3 CDN because the project environment cannot run npm. I have been trying to create a component with html+CDN, but when attempting to create a recursive component, it only renders the top-level element. Is there ...

Percy snap shot test cannot be executed in Testcafe due to the error message: npm ERR! The script 'test:percy' is

For my initial test with percy snapshot, I used the command below: npm run test:percy Unfortunately, an error message appeared when running the command: xxx.xxx@LPG002572 TC-Visual % npm run test:percy npm ERR! missing script: test:percy npm ERR! A com ...

Converting N-API object to C++ basic data types through reading

I've been working on a N-API module by customizing the ObjectWrap boilerplate provided in generator-napi-module. So far, I have successfully passed an array containing objects with string, number, and boolean properties from native C++ code to JavaScr ...

"Can you provide guidance on displaying a form for a specific element based on its unique ID

I am trying to display the edit form when clicking on a specific image, but it is currently showing for all tasks. I need help in figuring out how to make it show only for one task. I attempted to use useState to change the boolean value "active" to show ...

lint-staged executes various commands based on the specific folder

Within my project folder, I have organized the structure with two subfolders: frontend and backend to contain their respective codebases. Here is how the root folder is set up: - backend - package.json - other backend code files - frontend - p ...

Invoking a PHP function within a JavaScript file

I'm facing an issue with calling a PHP function from JavaScript. I have written a code snippet where the PHP function should print the arguments it receives, but for some reason, I am not getting any output when running this code on Google Chrome. Can ...

Activate the sliding animation for closing the modal window

I have successfully implemented a modal using a combination of html, css, and JavaScript. The code snippets are provided below for reference. One noticeable feature is that the modal window opens with a sliding animation effect from the top. However, I a ...

Vuejs: Limiting the number of items in an li tag to 5

selectPreviousSearch(index) { this.search = this.searchHistory[index]; this.showSearchHistory = false; }, <input class="form-control bg-light-blue" id="SearchText" type="text" v-model="search" @keydown.enter = 'ent ...

Standalone JSON Hyperlinks within a Table Column

My webpage features a table filled with information from a JSON API. Currently, I am struggling to create unique links in the far right column of the table. The issue is that all rows are linking to the same HTML page (12345), which is not ideal. What I w ...

Why is gh-pages not recognizing the project directory while looking for resources?

I've encountered an issue with my website that functions perfectly fine locally but when deployed on gh-pages, it results in several 404 errors while attempting to retrieve resources. One such example is a line of CSS for setting the background: back ...

Conceal the Div element in the loop if there is no image present

Im trying to set a condition in my WordPress loop. If there is no image, then the image box (.thumbHome{display:none}) should not appear. This is what I have in my function.php: function getThumbImages($postId) { $iPostID = get_the_ID(); $arrImages =& ...

Creating events in DayPilot Calendar

Currently, I am in the process of integrating Daypilot lite calendar into my project. Progress has been decent so far, but I have encountered a small obstacle. The issue arises when attempting to create a new event on the calendar - I am unable to pass an ...

Utilize JSCS formatting rules in VSCode for enhanced code styling

Can VSCode be integrated with JSCS to modify the default indentation spaces, customize the format option (Alt+Shift+F) to adhere to .jscsrc rules and presets while formatting JavaScript code? ...

What is the correct syntax for comparing -1, 0, and 1 in JavaScript?

Is there a way to achieve the functionality of a <=> b in JavaScript? It seems that this operator does not exist. Any suggestions for an equivalent function? Appreciate it, Dorian ...

What is the best way to retrieve data and handle error messages using the fetch API in Node.js?

I am attempting to utilize Node's 18 fetch API to send requests to a REST server. Below is the code snippet: const get = () => { let response = await fetch("10.0.0.12/test", { method: "GET", }); console.log(&quo ...

The data retrieved by jQuery AJAX is empty when accessed outside of the success handler

Here is a code snippet to consider: let source = null; fetch('https://example.com/data') .then(response => response.json()) .then(data => { source = data; console.log(source); }); console.log(source) When the fetch request ...