Toggle visibility on the child DOM element of each item in the v-for loop

Here's an example of a template:

<template>
    <table class="table table-hover">
        <tbody>
            <tr>
                <th style="width:260px">Info</th>
                <th>Details</th>
                <th>Actions</th>
            </tr>
            <tr v-for="(item, index) in items">
                <td>
                    <div>
                        <span class="hover-on-click-input">{{item.content.name}}</span>
                        <input class="form-control hidden" type="text" :value="item.content.name" @blur="updateInfo(item, 'author', $event.target.value)">
                    </div>
                </td>
            </tr>
        </tbody>
    </table>
</template>

items for v-for are loaded dynamically after the page has been loaded.

What is the best way with Vue2.js to hide the span element and show the input element when the span is clicked? Then if anywhere on the page is clicked (or the input loses focus), toggle the input back to hidden and unhide the span again. Plus, the value of the input will be sent to the server.

I have already read this: https://v2.vuejs.org/v2/guide/list.html
And this: https://v2.vuejs.org/v2/guide/class-and-style.html

The challenge here is that the item of the v-for loop is a <tr>, not a <span> or <input>, but changes need to be applied to the <span> and <input> elements. We can refer to these as "item's DOM child elements".

Thank you.

Answer №1

Include an editing attribute for every element. After that

<span class="hover-on-click-input" @click="element.editing = true">{{element.content.name}}</span>
<input v-if="element.editing" class="form-control hidden" type="text" :value="element.content.name" @blur="updateDetails(element, 'author', $event.target.value)">

Additionally, ensure to add element.editing = false inside the updateDetails function.

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

fetching indexeddb information using the equivalent of a "WHERE IN (a,b)" query

I've been working on transitioning from websql to indexeddb, but I'm struggling to recreate the SELECT query: "SELECT * FROM tableA WHERE cid ='"+cid+"' AND hid IN("+hid+",1) ORDER BY hid DESC LIMIT 1"; function getMyData(e) { var ...

Encountering a Babel error while attempting to compile my front-end assets using Laravel Mix. The module build process fails, causing

I've been struggling to fix this persistent error, trying various solutions from different sources including Git, but nothing seems to work. Error: Module build failed: this.setDynamic is not a function In my package.json file, I have the following ...

The server is failing to provide the requested data in JSON format

I am struggling with making a simple API call using Node.js as the backend and React in the frontend. My Node.js file is not returning data in JSON format, and I'm unsure of the reason behind this issue. I need assistance with two main things: Why is ...

What to do when the 'image' property in Next.js next/image has an implicit 'any' type and needs fixing?

I'm a newcomer to Next.js and TypeScript and I can't seem to find any helpful resources on resolving this particular issue: import Image from 'next/image'; export default function Item({ image }) { // <-- parameter image needs a &ap ...

Extract the property value and save it as an array in Vue

Looking to extract all values of a specific property and save them as an array. I attempted the following method: data() { return { roomList: null } }, methods: { getRooms() { var that = this axios.get('http://local ...

Utilize Angular service to deliver on a promise

Currently, I have a service that is responsible for updating a value on the database. My goal is to update the view scope based on the result of this operation (whether it was successful or not). However, due to the asynchronous nature of the HTTP request ...

Guide on editing the index html file within a Vue CLI project

During the build process of Vue CLI, I am looking to make changes to the index.html file (vue.config.js). Is there a way to achieve this? This is what my current index.html looks like: <!DOCTYPE html> <html lang="en"> <head> < ...

Challenges faced when attempting to launch a React-powered application on Railway platform

When I transferred my fullstack app (React + Express) from Heroku, I encountered an issue. React apps need to be built to run and require necessary dependencies installed, but typically only raw source code is stored on Git. A typical structure for fullst ...

How to Develop a WebSocket Client for Mocha Testing in a Sails.js Application?

I've been attempting to utilize the socket.io-client within the Mocha tests of my Sails JS application for making calls like the one shown below. However, the .get/.post methods are not being invoked and causing the test case to time out. var io = re ...

Turning off the MaskedEditValidator and MaskedEditExtender

I have created a compact table that allows users to edit, delete, and add records: The table (DataGridView, bound) is user-friendly: click on "Edit" to enter the editing mode (which includes a "Cancel" option), update values, and then click "Add" to inser ...

Tips on preventing repeated data fetching logic in Next.js App Routes

I'm currently developing a project with Next.js 13's latest App Routes feature and I'm trying to figure out how to prevent repeating data fetching logic in my metadata generation function and the actual page component. /[slug]/page.tsx expo ...

Efficiently loading components in Angular using browserify with lazy loading

As I work on developing the architecture of a complex application with Angular, I have started with the angular-seed project which seems to be a solid starting point. However, one issue that concerns me is how Angular apps tend to load everything upfront b ...

Using Jquery, Javascript, or Ajax to deactivate the iframe by clicking on it

Whenever a link in an iframe is clicked, I need to close the iframe and div elements. I have seen websites that are able to achieve this functionality, but I can't remember the specific URLs. A while ago, I copied this code from a website to detect ...

AngularJS ui-select not responding correctly to selected items

Currently, I am utilizing the ui-select module within AngularJS. <ui-select ng-model="tipData.category" search-enabled="false" name="category" required="required" class="orange-site-color-select custom-select"> <ui-select-match><span clas ...

Tips on maintaining a constant number of elements displayed within a container while scrolling using *ngFor

In an effort to improve the performance of loading a large amount of data inside a container div, I implemented a solution. It initially worked fine, but as I continued to append elements to the list while scrolling down, it started to slow down significan ...

The Chrome extension I created using JQuery to trigger 'keyup' events is not updating the value of the 'input' field on a website that utilizes knockout JS

I am currently working on developing a Google Chrome extension for a website that appears to be utilizing 'knockout JS'. Let's take a look at the HTML element below: <input type="text" class="form-control text-right" id="amount" autoco ...

I am experiencing an issue with Array.some not functioning properly within my React component, whereas Array.map is working

I am attempting to utilize Array.prototype.some() within my React component to check if a particular value exists in my array of objects. However, I keep encountering the error message data.some(...) is not a function. Interestingly, Array.prototype.map() ...

Explanation of Default Export in TypeScript

I recently started learning about JS, TS, and node.js. While exploring https://github.com/santiq/bulletproof-nodejs, I came across a section of code that is a bit confusing to me. I'm hoping someone can help explain a part of the code. In this project ...

Image displaying recreation of drop down lists similar to those found on Facebook pop-up windows

Seeking guidance on how to create popup drop-down lists similar to Facebook's "Who liked this link?" feature. I believe it involves the use of jQuery. Click here for an example image. Any suggestions, folks? ...

You can see the JavaScript code directly in the browser

I'm completely puzzled as to why this is happening. How strange that the JavaScript code is visible on the browser, almost appearing like regular text on the web page. This mysterious occurrence seems to be exclusive to Firefox. Despite scouring vario ...