The Vue toggle feature remains locked in place and cannot be toggled back, it only switches in one direction and cannot

I just can't seem to get this simple button toggle working properly. I have a button that starts with the text "Pause" and when clicked, it should change to "Resume". However, while it changes to "Resume" initially, it never toggles back to "Pause" when clicked again.

What am I missing here?

Here's my code:

<button class="btn btn-primary btn-block" v-on:click="pauseTask" type="button" role="button" id="" aria-expanded="false" style=" color: #6c757d border:none; border-radius: .15;">
    {{ pauseButton.text }}
</button>

Vue

data() {
    return {
        pauseButton: {
            text:'Pause'
        },
        isOpen: true
    }
},
pauseTask: function() {
      this.isOpen = !this.isOpen;
      this.pauseButton.text = app.isOpen ? 'Pause' : 'Resume';
    },

Answer №1

Seems like there is a typo in your code, possibly caused by the undefined app variable.

To streamline your code and avoid managing two separate data properties, I recommend using a computed property to determine which text to display.

new Vue({
  el: '#app',
  data: () => ({ isOpen: true }),
  computed: {
    pauseButtonText () {
      return this.isOpen ? 'Pause' : 'Resume'
    }
  },
  methods: {
    togglePause () {
      this.isOpen = !this.isOpen
    }
  }
})
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.min.js"></script>
<div id="app">
  <button @click="togglePause" type="button">
      {{ pauseButtonText }}
  </button>
</div>

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

Exploring AngularJS: Understanding the Differences Between $http's Success and Then

Can someone explain the difference between these methods for me? I am curious about the distinctions between .then and .success functions, as well as .error. Thank you. // Simple GET request example: $http({ method: 'GET', url: '/some ...

In search of assistance with creating a function that can transform an asynchronous function into a time-limited version

Let's discuss the challenge requirements: Given a function called fn that operates asynchronously and a time limit t in milliseconds, the goal is to create a new version of this function with a time constraint. This new function should behave accordi ...

Accessing data from arrays containing objects through an AJAX Request

I am trying to work on an ajax request, but I'm struggling with it. Here is the current ajax call I have: $.ajax({ type: 'GET', url: 'https://teamtreehouse.com/garrettsanderson.json', dataType: 'json', success: f ...

Tips for synchronizing with animation completion in ui-router's $stateChangeStart event

Incorporating AngularJs and ui-router in my project has been a smooth process. One particular requirement I have is to gently fade out the current view when a user clicks on a link, before navigating them to another view. To achieve this, I have created ...

Selecting items using raycasting

Currently attempting to select objects when clicked, using the common code found in various examples: function onMouseDown(evt) { evt.preventDefault(); canvasAbsoluteHeight = $('canvas').height(); canvasAbsoluteWidth = $('canv ...

Tips for changing information in a child document using mongoose

My task involves updating the value of tQuan to 15 where the tName is FBK in the stocks array. I have been searching for a solution without success so far. It's important to note that I must adhere to the existing schema design without making any chan ...

Redirect in Vue once store information is obtained

Trying to create an edit page at /edit/:id where the input value is retrieved from the Vuex store. However, if there is no task in the store with the same id as the params, I need to redirect to /404. How can I make this happen? I attempted using the cre ...

The binding of a bound element within an ngIf directive does not automatically update

I recently developed an AngularJS directive where I included an ngIf directive in the template. Within this ngIf directive, there is an input element which is bound to the scope of my directive. <div ng-if="bool"><input ng-model="foo"></div ...

Move a div by dragging and dropping it into another div

Situation Within my project, there is a feature that involves adding a note to a section and then being able to move it to other sections, essentially tracking tasks. I have successfully implemented the functionality to dynamically add and drag notes with ...

Trouble with select box initialization resolved

I'm having trouble retrieving the selected value from a dropdown menu as it always returns the first value. Initially, the dropdown is empty. When I press a button to open the modal and populate the dropdown with options, it seems that the issue aris ...

Trouble with split() in jQuery/JavaScript - A straightforward issue

if ($(".productpage .description").html() != null) { var textToHide = $('.productpage .description').html().split('<br class="breakHere">')[1]; var visibleText = $('.productpage .description').html().split(&apos ...

Can you share the outcomes of executing a Node.js program in real-time?

Is there a method to execute a program (such as tcpdump) and have nodejs capture the console output in real-time to display in an HTML format without saving it? I am interested in running a program that displays information in the console, with the capabi ...

Secure this Ajax reply

Is there a way to speed up the Ajax response time for notifying the user on the client side that the username is already taken, or is this delay typical behavior for Ajax? Client: <title>Choose a username</title> < ...

Integrating eBay API with Node.js

Hello, I am new to Node.js and I could really use some assistance with exporting console log data to an HTML page. I came across a helpful example on GitHub at this link: https://github.com/benbuckman/nodejs-ebay-api My current issue is that although I h ...

Develop universal style classifications for JSS within material-ui

Currently, I am utilizing the JSS implementation of material-ui to style my classes. As I have separated my components, I find myself dealing with a significant amount of duplicated code in relation to the components' styles. For instance, I have mu ...

Is there a way to position one DIV behind another?

Hey, I'm working on my first project and running into some trouble with divs. I'm trying to position the firework behind the central text but can't figure it out. Can anyone lend a hand? I need to add more details in order to submit the que ...

Issue with generating random cells in a table using a loop

Within my HTML code, I have a table constructed using the table element. To achieve the goal of randomly selecting specific cells from this table, I implemented a JavaScript function that utilizes a for loop for iteration purposes. Despite setting the loop ...

Transform JSON code from JQuery to PHP

Currently, I am in the process of translating a code snippet from JQuery to PHP for performing a json POST request to a remote server. Here is my original Jquery code: $( document ).ready(function() { $('#button').click( function() ...

What does this Javascript error mean?: TypeError - Trying to read the 'id' property of an undefined value

My React-Redux app is functioning well, able to interact with an API for fetching, posting, updating, and deleting data. However, a window containing an error message appears before the rendered data, stating: TypeError: Cannot read properties of undefined ...

Learn how to dynamically change the location of a Google Map using React

I have a Google Map component where the main component sends props (coordinates as an array) to display a new location on the map. However, I am facing an issue where the map does not refresh even though the coordinates are changing correctly and being rec ...