In a Vue.js component, the brilliance of stars dims once they are clicked

Currently, I am working on creating a star-rating component using Vue.js. The issue I am facing is that when I click on the stars, they fade out before displaying the final result. You can find the JSFiddle link below:

https://jsfiddle.net/L6L34ybr/5/

This behavior differs from the effect showcased in the code I mostly copied from:

http://codepen.io/nouveller/pen/qOxKBJ

I suspect that the issue may be related to the rating property.

Currently, I have inline bound the class like so:

:class="{selected: ((value >= rating && value != null))}
.

However, I would prefer to bind it with an object using something like

:class="selected: someFunction(rating)"
, and then define the someFunction(rating) in the computed property or another location outside of the html. Is there a way to achieve this?

Answer №1

Instead of

@click="setRate(rating)"

try defining your click event like this:

@click.prevent="setRate(rating)"

By doing this, you can prevent the click event from triggering twice, once for the label and once for the input.

Regarding your second question, you can include your someFunction() in the methods object of your component like so:

methods: {
    someFunction: function (value, rating) {
        return value >= rating && value != null;
    }
}

Then you can use it in your markup as shown below:

<label :class="{selected: someFunction(value, rating)}">

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

Preserve marked checkboxes upon page refresh

So I created a search engine with a filter using checkboxes in a form. The filter function is working fine when I submit the form (I'm using Flask for this). However, once the results are displayed, the checkboxes that were used as filters become unch ...

"Optimizing navigation with dynamic link routing in AngularJS

I am working on a list of apartments displayed using ng-repeat. I need each apartment to be a clickable link that directs the user to view more details about that specific apartment. However, I am facing an issue where the links are not being repeated dyna ...

Is there a way to transfer table data from one table to another table using AngularJS?

I have a situation where I need to transfer data from one table to another. There are four buttons available: Right, AllRight, Left, and AllLeft. When the Right button is clicked, only the selected row should move to the left side table. When the AllRight ...

Effort to update Div element with Ajax fails to function

Looking for some assistance here. I'm attempting to update a database's entries using Ajax after submitting a form. The entries are displayed within a div. I've tried the following code to refresh only that specific div, but it doesn't ...

Hide modal once form has been successfully submitted

Is it best practice to pass handleClose into ForgotPasswordFormComponent in order to close the modal after form submission, or is there a better way to achieve this? <StyledModal open={openModal} onClose={handleClose} closeAfterTransition slots={{ bac ...

Is there a way to remove unnecessary code from the end of a string using JavaScript?

Most of the users of my script are hosted on a server that inserts a text ad at the end of each page. Unfortunately, this code is appearing in my script's AJAX responses as well. The unwanted content is an HTML comment followed by a link to their sign ...

Tips for concealing a component in the DOM using ng-if in angular.js and angularmaterial:

Currently, I'm utilizing angular.js 1.5 along with angular material on the frontend to populate the DOM with objects retrieved from the Database. Within one of the repeaters, I am facing an issue where I need to determine if an object is already prese ...

Is it possible to view the mousehover action in real-time on the browser screen when using Selenium?

Here is my code snippet: WebElement menu= bd.findElement(By.cssSelector("#main-nav > div.multi-level-nav.reveal-on-click > div > ul.first-level-ul > li:nth-child(1) > a")); actions.moveToElement(menu).build().perform(); ...

Is a fresh connection established by the MongoDB Node driver for each query?

Review the following code: const mongodb = require('mongodb'); const express = require('express'); const app = express(); let db; const options = {}; mongodb.MongoClient.connect('mongodb://localhost:27017/test', options, fu ...

I can't seem to retrieve any values from the API other than "chicken"

Is there a way to make the search bar in my recipe app look for recipes that I provide, rather than fetching data from useState? Any suggestions on how I can achieve this? import React, { useEffect, useState } from 'react'; import Recipe from &ap ...

AngularJS Error: [$injector:modulerr] - I'm new to this

I am not utilizing ngRoute or any Angular service that requires injection. I am injecting my own module and controller as necessary. However, I am still encountering the following error in the console: Uncaught Error: [$injector:modulerr] http://errors.an ...

Saving data in a function component's state variable

When working with a React class component, adding a property is straightforward: export default class Thing extends Component { constructor(props){ super(props) this.hasRunQuery = false } handleClick = () => { this ...

Inserting Text with Line Breaks into a Textarea Using jQuery and PHP MySQL

I am dealing with a form that users complete with a textarea input. This form is saved into a MySQL database, and later the user has the ability to make edits. The problem I am facing is that whenever I try to use AJAX to provide suggestions for the textar ...

leveraging dependency injection to retrieve a function in JavaScript

I'm exploring a way to obtain a function in JavaScript without executing it, but still defining the parameters. My current project involves creating a basic version of Angular's dependency injection system using Node.js. The inject() method is us ...

Efficient method for transferring element text to title attribute using AngularJS

I currently have a similar setup within a table: <tr ng-repeat="book in books"> <td title="{{book.title}}">{{book.title}}</td> <td title="{{book.author}}">{{book.author}}</td> </tr> To handle overflow, I am using C ...

Deciding Between Promises and Callbacks in Node.js

As I update my older Node.js code, I am also creating new modules to integrate with the existing code. One challenge I face is transitioning from using callbacks to utilizing ES6 promises more frequently. This has created a mixture of functions that return ...

Utilizing external objects within the data() function of Vue: A comprehensive guide

When it comes to defining data in a Vue component, the typical approach is to use the data function as shown below: data() { return { a: 0 } } But I have a question: instead of defining the data object after return, is it possible to defin ...

Encountered an issue with trying to access the 'map' property of an undefined value while using Express and VueJS

Currently, I am in the process of developing a fullstack application utilizing Express, VueJS, and Mongoose. The app functions as a news feed platform. A couple of days ago, I encountered an error which was resolved with the help of your guidance. However, ...

Error: Attempting to access index '0' of an undefined JSON object in a ReactJS application

I'm encountering an issue with my first React app where I can't retrieve data from a JSON API. The strange thing is that I've used this API successfully before, but not within a React context. Can anyone pinpoint what might be wrong in my co ...

JQuery datepicker is functioning properly only after an alert when loaded with AJAX

Here's a puzzling question I have encountered. I implemented a field with a datepicker functionality. This field gets loaded into a div through an AJAX call. To sum it up, everything seems to be in working order.. But there's a strange issue. I ...