Modify the appearance of each button based on the target value through the "Change Target Value" option

My task involves adjusting the background color of a button based on changes in the target value. Initially, I set the target value to "10". When I click my "1" button and decrease it to "9", the background color of the button changes to "red" because the input value is now lower than the target value. However, I encountered an issue when I attempted to change the target value by using the "Change Target Value" button. The trigger was supposed to re-evaluate the target value against the button's value, updating the background color based on the arithmetic operator. Unfortunately, my code is not functioning correctly. Can someone please assist me?

https://i.sstatic.net/06s3c.png

// JavaScript code snippet
// Get the modal
var modal = document.getElementById('myModal');
// Get all buttons except reset and submit buttons
var btns = document.querySelectorAll('button:not([id=reset]):not([id=submit])');
// Set initial target value
var TV = document.getElementById('inputtarget');

// Event listener for each button to open modal
for (var i = 0; i < btns.length; i++) {
  btns[i].onclick = function() {
    TV.setAttribute('startbtn', this.id);
    modal.style.display = "block";
  }
}

// Additional JavaScript code continues...


#b1,
#b2,
#b3 {
  /* Button styling */
}

/* Additional CSS styling goes here... */

HTML elements related to the buttons, inputs, and modals are outlined within the code snippet.

Answer №1

It seems like there may be a small error in your for loop logic. Here's a slightly revised version of the event handler section:

// Modify Target Value
var MTV = document.getElementById('trigger'); 
var buttons = document.querySelectorAll('button:not([id=reset]):not([id=submit])');

MTV.onclick = function () {
  for (var j=0; j < buttons.length; j++) {
    var button = buttons[j];
    if ( button.innerHTML<targetvalue ){
      button.style.background='red';
    }
    else if ( button.innerHTML>=targetvalue ){
      button.style.background= 'green';
    }
    else {
      button.style.background= '';
    }
  }
}

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

The marker on the Three JS globe is constantly visible inside the globe or positioned far outside its boundaries

As a beginner in programming and React three fiber, I am struggling to understand why the marker I place on the globe using geolocation data won't stay attached. Despite my attempts to convert latitude and longitude to Cartesian coordinates, the marke ...

Using ESLint to enforce snake_case naming conventions within TypeScript Type properties

When working with TypeScript, I prefer to use snake_case for properties within my Interfaces or Types. To enforce this rule, I have configured the ESLint rule camelcase as follows: 'camelcase': ["error", {properties: "never"}], Even though the E ...

Exploring JSON Parsing in JavaScript

Upon processing the following JSON data: foobar({ "kind": "youtube#searchListResponse", "etag": "\"I_8xdZu766_FSaexEaDXTIfEWc0/pHRM7wJ9wmWClTcY53S4FP4-Iho\"", "nextPageToken": "CAUQAA", "regionCode": "PL", "pageInfo": { "totalResults": 68 ...

Comparison of performance between serializing an object to indexedDB and using JSON.stringify

I am curious about the efficiency differences in terms of browser/CPU/memory between using JSON.stringify for serialization versus writing an object to an object store in indexedDB. The context for this question is optimizing the process of writing an obj ...

Unable to view the line number of a CSS style in Chrome Dev Tools while inspecting an element anymore

Today, a strange issue arose that has me puzzled. I can't seem to figure out what caused it. If you take a look at this picture here: This screenshot is from Twitch where I had a tab open. You can see the CSS file and line number displayed as usual. ...

What is the proper way to incorporate html entities in my specific situation?

In my application, I am attempting to incorporate double macron characters. My current method involves using the &#862; entity, like this: t&#862;t, which results in t͞t. However, I have encountered issues with reliability, as sometimes the ...

ng serve issue persists even after resolving vulnerabilities

Can anyone assist me in resolving why I am unable to start my project after fixing 3 high vulnerabilities? I ran npm audit to identify the vulnerabilities and then used npm install --save-dev @angular/<a href="/cdn-cgi/l/email-protection" class="__cf_em ...

What is the best way to limit the number of options displayed in the vue-multiselect

I'm looking to truncate multiple values on the vue-multiselect component. I attempted to override various classes without success, as shown in this example: .multiselect__content-wrapper { overflow-x: hidden; text-overflow: ellipsis; } ...

Create an onClick event for a dynamically populated div element using React

I am currently facing an issue with my React app. I have multiple div elements that are dynamically loaded with information from a database. My goal is to create a pop-up window that displays more detailed data related to each specific div when clicked. Ho ...

Find the variance between today's date and a selected date, then initiate the timer based on this variance

I have a grid containing data. I utilize the loadGrid() function to preload data or conditions before the grid finishes loading. When the active state is set to 1, my intention is to initiate a timer by calculating the difference between the current date ...

Using React-Bootstrap to create smaller user inputs

Is there a way to reduce the size of the Input element? By default, the syntax looks like this: <Input type='text' value='' placeholder='Enter Entity'/> Using bsSize='xsmall' does not work for making the inp ...

Is it possible to customize the appearance of the Facebook Like box with a scroll-bar?

I recently added a Facebook like-box to my website, and I'm trying to customize the scroll bar within the iframe. However, I'm struggling to find a way to do so. Does anyone have any ideas on how this can be achieved, or if it's even possibl ...

Inserting an image into a <div> means adding a reduced-size image numerous times to fill the Div

I have encountered an issue while using the code provided in the CodePen link below to paste an image into a Div. The problem is that when I paste a small image, it shows up multiple times within the Div if clicked on after pasting. My goal is to display t ...

Mastering Backbone views and router functionality is essential for building scalable and efficient web

In my code, I have a series of page states that mimic the steps of a shopping cart checkout process. The first set of code sets up the ItemsCollection, ItemsView, and ItemsApp in Backbone.js. var ItemsCollection = Backbone.Collection.extend({ model: I ...

Alert Box Displays Variable Name Instead of Label Name in Form Validation - MM_validateForm()

Looking at the screenshot, you can see variable names such as "email_address", "email_message" and "email_subject". I would like these to be displayed as "Email", "Message" and "Subject" instead. The validation in this form is done using MM_validateForm() ...

Can Javascript work together with HTML?

Can 'this' be utilized in this specific scenario? var td = document.getElementsByTagName("td"); for (var i = 0; i<td.length; i++){ td[i].id = 'abc' + i; }; for (var i = 0; i<td.length; i++){ td[i].onclick = c ...

Cannot load the next page using jQuery ajax

I am working on a project with an index.html file that includes a nav menu, header, footer, and a div.content where jQuery is used to parse the html of page1.html. Initially, page1.html loads perfectly. However, I encountered an issue when trying to imple ...

What is the best way to insert a space within a stationary element?

I'm facing an issue with two fixed elements positioned at the bottom of my webpage: #wrapper { position: fixed; background: gray; color: #fff; bottom: 0; left: 0; right: 0; border-radius: 12px 12px 0 0; width: 100%; } #bottom-eleme ...

"Update data on a webpage using Javascript without the need to refresh the

I encountered a problem with my code for posting messages in the "chatbox". When I include return false; at the end of the function, the data is not submitted. However, if I remove it, the data submits successfully. JavaScript Code function dopost() { ...

What could be the reason for an ASP.NET Core application not loading within an iframe on the same domain?

I am facing an issue with my ASP.NET Core MVC website, which is embedded as the src of an IFRAME within a portal. Both the portal and the .NETCore application share the same domain (e.g., site.portal.domain / portal.domain). Upon entering the portal, I en ...