I've written the following which does not work and I wonder if it's possible to follow the DRY principle in this Vue code:

`const app = Vue.createApp({
    data(){
        return {
            firstInputBoxTextValue: "",
            secondInputBoxTextValue: ""
        };
    },
    methods: {
        showAlert(){
            alert("Button has been clicked..!");
        },
        updateInputBoxText(event,inputBoxTextIdentifier)
        {
            this[[inputBoxTextIdentifier]] = event.target.value;
        }
    }
});
 
app.mount("#assignment");
 
 
//////////////////////////////////////
 
 
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Vue Basics</title>
    <link
      href="https://fonts.googleapis.com/css2?family=Jost:wght@400;700&display=swap"
      rel="stylesheet"
    />
    <link rel="stylesheet" href="styles.css" />
    <script src="https://unpkg.com/vue" defer></script>
    <script src="app.js" defer></script>
  </head>
  <body>
    <header>
      <h1>Events</h1>
    </header>
    <section id="assignment">
      <h2>Event Practice</h2>
      <!-- 1) Show an alert (any text of your choice) when the button is pressed -->
      <button v-on:click="showAlert">Show Alert</button>
      <hr />
      <!-- 2) Register the user input on "keydown" and output it in the paragraph (hint: event.target.value helps) -->
      <input type="text" v-on:keydown="updateInputBoxText($event,firstInputBoxTextValue)"/>
      <p> OUTPUT {{firstInputBoxTextValue}} </p>
      <hr />
      <!-- 3) Repeat 2) but only output the entered value if the ENTER key was pressed -->
      <input type="text" v-on:keydown.enter="updateInputBoxText($event,secondInputBoxTextValue)"/>
      <p>OUTPUT {{secondInputBoxTextValue}}</p>
    </section>
  </body>
</html>`

Tried looking online/on StackOverflow/using ChatGPT - would like to be able to find a data attribute using the value of an argument in a method, updating the

value as follows:const app = Vue.createApp({
    data(){
        return {
            firstInputBoxTextValue: "",
            secondInputBoxTextValue: ""
        };
    },
    methods: {
        showAlert(){
            alert("Button has been clicked..!");
        },
        updateInputBoxText(event,inputBoxTextIdentifier)
        {
            this[[inputBoxTextIdentifier]] = event.target.value;
        }
    }
});
1

There are 1 best solutions below

0
moon On

I never seen [[]] before in a vue or js file,its not a standard grammar of JavaScript,and the right way is below

<input type="text" v-on:keydown="updateInputBoxText($event,'firstInputBoxTextValue')"/>
...
updateInputBoxText(event, inputBoxTextIdentifier) {
  this[inputBoxTextIdentifier] = event.target.value;
}