I'm currently working on an app that allows users to add strings to a list and edit them. When the user clicks on the "edit" button, the <h3>
element transforms into an input field using v-if/v-show conditional rendering. I want to enhance this functionality so that when the input field appears, it automatically gets focused.
Below is the code snippet:
<div class="card" v-for="(item, index) in list" :key="index">
<!-- not editing -->
<div v-if="editing != index + 1">
<button class="edit-btn" @click="setEditing(item, index)">
edit
</button>
<h3 class="title">{{ index + 1 + ". " + item }}</h3>
<button class="delete-btn" @click="deleteEntry(index)">
bin
</button>
</div>
<!-- editing -->
<div v-show="editing == index + 1">
<button
class="edit-btn"
style="background-color:white;color:grey;border-color:grey;font-weight:bold"
@click="cancelEdit"
>
x
</button>
<input
ref="editInput"
autocomplete="off"
@change="console.log(entry)"
class="edit-input"
id="edit-input"
@keyup.enter="saveChanges(index)"
v-model="entry"
/>
<button
class="delete-btn"
style="background-color:white;border-color:green;color:green"
@click="saveChanges(index)"
>
mark
</button>
</div>
</div>
function
setEditing(entry, index) {
this.editing = index + 1;
this.entry = entry;
var el = this.$refs.editInput[index];
console.log(el);
el.focus();
// document.getElementById("edit-input").focus();
},
variables
data() {
return {
editing: 0,
newEntry: "",
list: [],
error: "",
entry: "",
};
},