AngularJS score tracker malfunctioning

Can you please review this for me?

http://plnkr.co/edit/i4B0Q2ZGiuMlogvwujpg?p=preview

<input type="radio" name="op_0" ng-value="true" ng-model="n1"> True
<input type="radio" name="op_0" ng-value="false" ng-model="n2"> False
<input type="radio" name="op_0" ng-value="true" ng-model="n3"> True
<input type="radio" name="op_0" ng-value="false" ng-model="n4"> False

Could you explain why two radio buttons are being selected with just one click? And also, why isn't the score value increasing or decreasing as expected?

Answer №1

For all radio buttons to function as a group, they must share the same name attribute. The distinguishing factor should be the value assigned to each button. If the names are different, selecting one will not deselect the others.

app.controller('aCtrl', function($scope) {

  $scope.s = 0;

  $scope.updateScore = function(val){
    if(val == 'true'){
      $scope.s += 1;
    }
    if(val == 'false'){
      $scope.s += -0.5;
    }
  }

});

 <div class="col-md-5 col-xs-8">
        <input type="radio" ng-click="updateScore('true')" name="op_0" ng-value="true" ng-model="n1"> True
        <input type="radio" ng-click="updateScore('false')" name="op_0" ng-value="false" ng-model="n2"> False
        <input type="radio" ng-click="updateScore('true')" name="op_0" ng-value="true" ng-model="n3"> True
        <input type="radio" ng-click="updateScore('false')" name="op_0" ng-value="false" ng-model="n4"> False
      </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

Getting Started with the Basic Example of Redux-Router

Recently, I decided to give redux-router a try and wanted to run the basic example. However, when I tried running the command: npm start I encountered an error message that said: node: bad option: -r Being new to the JavaScript modern ecosystem, I&apos ...

Is it possible to utilize Dictionaries (key, value) collections with JQuery?

Is jQuery capable of supporting a collection of Dictionaries which consists of keys and values? I am interested in organizing the following data into a structured format: [1, false] [2, true] [3, false] This structure should allow for adding, looking up ...

Unable to run any npm scripts in a React project

My React application has been running smoothly for a while, but recently all the npm commands in the package.JSON file have stopped working. { "name": "fitness-appication-frontend", "version": "0.1.0", "private": true, "dependencies": { "reac ...

"Deactivate the escaping feature in PHP when using heredoc syntax

My PHP code generates minified JS output using the heredoc. Take a look at this snippet: function prerefresh(){$("#len").empty();predata.forEach(item)} I've highlighted that the {$ is causing issues with variable escaping in my heredoc. Is there a ...

<head> Javascript codes are failing to run

Currently utilizing JQuery Mobile, my goal is to navigate between pages with minimal script loading each time a page loads. Essentially, I want to import all the general scripts (such as JQuery.js, jquery_mobile.js, main.js, etc.) ONCE for all pages. In m ...

Tips for passing an object as an argument to a function with optional object properties in TypeScript

Consider a scenario where I have a function in my TypeScript API that interacts with a database. export const getClientByEmailOrId = async (data: { email: any, id: any }) => { return knex(tableName) .first() .modify((x: any) => { if ( ...

How can Angular.js directives help display various nested data structures?

Is there an Angular.js directive available for displaying nested JSON data dynamically as a view without prior knowledge of the structure? Let's say we have the following JSON resource at a REST endpoint /api/orgranisations/:organisation_id/: { ...

The autofocus feature on the textarea in Angular Material Dialog seems to be malfunctioning

Within a web app, I am utilizing the Dialog component from Angular Material. The Dialog consists of only a textarea that is initially empty. I aim to automatically focus on the textarea when the user opens the modal dialog. How can I achieve this? Despite ...

Unable to find any matches when conducting a search through Google Apps Script

After spending days analyzing the code, I am encountering an error that states "uncaught reference error: google is not defined." This issue seems to occur specifically in line 360. Curiously, when inspecting the original code editor, line 360 simply conta ...

Issue with loading Three.js asynchronously

My attempt to determine the maximum value of a point cloud data using the following code proved unsuccessful. import { PLYLoader } from "three/examples/jsm/loaders/PLYLoader"; let max_x = -Infinity; function initModel() { new PLYLoader().load ...

Is `h` equal to `createVNode` function?

Both h and createVNode are exposed from vue. The documentation on this page seems to imply that they are interchangeable: The h() function is a utility to create VNodes. It could perhaps more accurately be named createVNode(). However, replacing h with ...

Animation triggered by scrolling is not functioning/displaying div

I'm attempting to make a div fade up when it enters the viewport by using the library found at https://github.com/michalsnik/aos Unfortunately, all that seems to happen is that the div gets hidden. In the head section of my HTML file, I've refe ...

What is the correct procedure for utilizing variables in the parseJson function when working with string objects?

While working with the parseJson function, I have encountered a challenge where I need to incorporate a variable within three objects. Is there a way to merge a variable with the value of one object? Alternatively, can I utilize a third object to store t ...

Choose particular ng-repeat elements to emphasize them in a different location

I am currently working with a list that is being parsed by an ng-repeat directive. The user has the ability to filter this list, which is functioning correctly. One particular feature I am trying to implement is highlighting the first item in the filtered ...

Tips for updating the First object based on certain matching values from the Second object using JavaScript

I am faced with the task of updating the listOfStudents Object by matching it with the homeworkResults Object based on their corresponding email values. Through comparison, when the email matches between the two Objects, I aim to retrieve the topic and suc ...

Hide the scrollbars on the sidebar

I'm attempting to recreate a sleek scrollbar that caught my eye on the website . To achieve this, I need to have a screen larger than 955px in order to display the sidebar. However, when my sidebar overflows in the y direction, it causes an additional ...

Exploring React-Query's Search Feature

Looking for guidance on optimizing my Product search implementation using react-query. The current solution is functional but could be streamlined. Any suggestions on simplifying this with react-query would be greatly appreciated. import { useEffect, use ...

Save the data from the JSON file when it is loaded

Currently, my factory is set up to load a JSON file. angular.module("app").factory("RolesFactory", ['$http', '$q', function($http, $q) { var d = $q.defer(); $http.get('events.json').success(function(data) { d. ...

The Google Pie chart is displaying outside of the designated div area when placed inside a dropdown menu

Encountering an issue with my pie chart rendering outside of its parent div when placed within a dropdown menu. The chart successfully loads after the page is loaded, but only displays correctly if I hover over the dropdown and allow it to load. If I do ...

javascript triggering before its designated event happens

I've encountered a puzzling issue with a complex file where a javascript function is behaving unexpectedly—it seems to be firing before its designated event actually takes place. I have ruled out the possibility of an onload event triggering this be ...