Hold off in Vue 3 component until button is clicked

Hello, I'm currently attempting to await a button click within a vue3 method. My goal is to trigger a function by clicking a button and then wait for the user to click either the "confirm" or "cancel" button. I am utilizing the vue3 developer CDN for this project. Here's my current code snippet:

<div v-if="hasAccess('FTO') && !selectedPlayerLoading" id="ftosheet-container">
    <div class="ftosheet-title noselect">FTO Sheet 
        <span v-if="hasAccess('FTO')" v-on:click='editFTOSheet()' style="float: right;">
            <i class="fas fa-edit"></i>
        </span>
    </div>

    <textarea class="ftosheet-text" v-model="selectedPlayer.ftosheet" :readonly="editingFTOSheet === false">
    </textarea>
    <div id="ftobutton-container">
        <button v-if="editingFTOSheet" id="saveftobutton">Save FTO Sheet</button>
        <button v-if="editingFTOSheet" id="cancelftobutton">Cancel</button>
    </div>
</div>
OnButtonPress() {
    Vue.nextTick(function () {
        return new Promise((resolve) => {
            const confirm = document.getElementById('saveftobutton')
            confirm.addEventListener('click', function() {
                resolve(true);
            });
            const deny = document.getElementById('cancelftobutton')
            deny.addEventListener('click', function() {
                 resolve(false);
            });
        });
    })
},

async editFTOSheet() {
    this.editingFTOSheet = !this.editingFTOSheet;
    if (this.editingFTOSheet) {
        const uneditedSheet = this.selectedPlayer.ftosheet;
        const buttonPress = await this.OnButtonPress();
        console.log(buttonPress);
        //TODO: Make work with await button press
    }
},

Due to the fact that the buttons are only rendered after clicking the "edit" button, I believe I need to use nextTick here? VS Code also indicates that await has no effect in this context. The console prints undefined immediately after calling the OnButtonPress() function. Please feel free to reach out if you require more information. Thank you!

Answer №1

        PressOnButton() {
        return new Promise((resolve) => {
            Vue.nextTick(function () {
                const saveBtn = document.getElementById('saveftobutton')
                saveBtn.addEventListener('click', function() {
                    resolve(true);
                });
                const cancelBtn = document.getElementById('cancelftobutton')
                cancelBtn.addEventListener('click', function() {
                    resolve(false);
                });
            });
        });
    },

The positioning of the return statement had to be adjusted prior to Vue.nextTick.

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

Troubleshoot: How to Fix the socket.io net::ERR_CONNECTION_TIMED_OUT

I am facing an issue with my website's real-time chat functionality. It works perfectly on localhost without any errors. However, when I try to run it on the server, I encounter the following error: http://my.domain:52398/socket.io/?EIO=3&transpo ...

How can I display a nested dropdown list within a form using JSON data in React?

Utilizing material ui and formik, I have integrated a form that requires a selection box with nested options to be displayed as shown in the image linked here. (The value selected needs to be submitted upon form submission) https://i.sstatic.net/02AI2.png ...

ReactJS rendering Express data as numbers instead of JSON format

My challenge involves sending strings from my Express server as a JSON object and then displaying these objects in my React app. Currently, instead of showing the expected data, it's simply displaying numbers for each of the 25 elements: 01234567891 ...

Generate a four-dimensional array populated with the data retrieved from an Ajax request

In my JavaScript code, I have an array that looks like this: var data = [ { y: '2017-01', a: 50, b: 90, c:110}, { y: '2017-02', a: 65, b: 75, c:120}, { y: '2017-03', a: 50, b: 50, c:10}, ...

Angular.js and DAO design pattern

Admitting my newness to Angular.js and lack of experience with frameworks like Backbone or Knockout, I am venturing into building an application that interacts with a server using a RESTful API. After delving deep into the angular documentation and blog po ...

The inclusion of react-helmet in my Gatsby website is leading to an issue where the element type is considered invalid

Incorporating the Layout component in my app has significantly improved the styling. This is where I introduced react-helmet. This is how the component looks: import React from 'react'; import { Global, css } from '@emotion/core'; im ...

Using ng-repeat on a select element is replacing the original content

I'm struggling with the following code snippet: <select> <div class="arrow-up"></div> <option data-ng-repeat="x in y">{{x}}</option> </select> the resulting output doesn't show the arrow div like this ...

Encountering a "Property does not exist on type" error when transitioning from JavaScript to TypeScript

Greetings, I recently made the transition from JS to TS for improved coding practices. However, TypeScript is throwing me these errors- the Validate function worked flawlessly in JavaScript. Property 'user' does not exist on type '{ isValid: ...

Is it possible to change the background color using jQuery?

Can you assist me with this: I have a website and I am looking to modify the bg-coler when hovering over the menu (similar to how it works on the rhcp-website). I attempted using jquery for this, but unfortunately, it did not yield the desired result.. ( ...

Issues encountered when packaging React Native iOS application with CocoaPods

Currently, I am in the process of developing an app with the assistance of https://github.com/invertase/react-native-firebase. The recommended method for installation is through CocoaPods, but I have encountered a multitude of issues while trying to archiv ...

Updating the React state of a function that is enclosed inside useRef does not result in any changes

I am facing an issue with accessing React state within a function that is enclosed in a useRef. Despite trying to use a helper function bound to App to access the state, the state does not update as expected inside the useRef function. getCount outside 0 ...

Issue with a Jquery toggleClass function not working properly on hover

Hello everyone! I am a newbie in the community and relatively new to the world of web programming. I am encountering an issue that is really frustrating me. I attempted to use this code to add a class when hovering over the ul list element so that I can d ...

Efficiently handling and sifting through vast amounts of numeric data in JavaScript

I am working with a collection of RGB value integers ranging from 0 to 255, organized into eight separate lists. For example, one list contains 8,349,042 values, equivalent to 2,783,014 RGB sets of three. The main goal is for the user to select a pixel in ...

Verifying phone numbers with express.js

I am looking to incorporate phone number authentication in my express.js API, similar to apps like WhatsApp or Telegram. While I have experience with passport.js, I haven't come across a strategy specifically for phone numbers. My idea is to generate ...

What is the best way to make the chips within a disabled v-select interactive?

I am facing an issue with a v-select element in Vue.js that uses v-chips to represent selected items. I want the chips to act as buttons when not editing the selection, but the v-select is disabled in non-editing mode. Unfortunately, due to disabling the v ...

What are some ways to find your way without using the Navigator app?

Navigator.js const App = createStackNavigator({ screenA: { screen: ScreenA }, screenB: { screen: ScreenB } }) const Navigator = createAppContainer(App); export default Navigator; App.js return ( <SafeAreaView> <Provider store={ ...

Help! My ReactJS D3 scatter plot is malfunctioning when I hover over it. What could be causing this issue and how

Everything looks great with the visualization until I hover over one of the circles, then it breaks. Check out the visualization in Codepen This is the error shown in the console: Uncaught TypeError: d.Time.substring is not a function The issue seems to ...

Encountering Timeout Issue Following Laravel 5.6 Framework Update

Yesterday, a strange issue suddenly appeared in my Laravel 5.6 project. It all started after I ran a composer update command. Since then, any Ajax requests to the project API, handled by Axios from a Vue component, are not functioning properly. No CRUD ...

Is there anyone available to assist me with this contact validation form?

I'm struggling to make this contact form highlight red and display 'not valid' inside the boxes. Despite my efforts, I just can't seem to get it to work! I'm unable to change the existing HTML tags, but I can add to the HTML. I wan ...

Vue.js - triggering an event handler

I am attempting to create links within the <td> tags in the second column. Upon clicking these links, I desire for the MonitoringTable function to be called with the specified value. Vue.Js: const app = new Vue({ el: '#page-wrapper', ...