What steps should I take to correct the color selection of a button that has been pressed down

Here is the code snippet that I am currently working with:

HTTP:

<label class="instructions" for="hidden_x"> Insert X: </label>
<button type="button" class="button" name="button_x" value="1" id="x_1" onclick="document.getElementById('hidden_x').value = 1"> 1 </button>
<button type="button" class="button" name="button_x" value="2" id="x_2" onclick="document.getElementById('hidden_x').value = 2"> 2 </button>
<button type="button" class="button" name="button_x" value="3" id="x_3" onclick="document.getElementById('hidden_x').value = 3"> 3 </button>
<button type="button" class="button" name="button_x" value="4" id="x_4" onclick="document.getElementById('hidden_x').value = 4"> 4 </button>
<input type="hidden" name="hidden_x" id="hidden_x" value=""> <br>

JavaScript:

window.onload = function(){
let buttonsX = document.getElementsByName("button_x");
for (let i = 0; i < buttonsX.length; i++) {
    buttonsX[i].onclick = function (event) {
        document.getElementById("hidden_x").value = event.target.value;
        buttonsX.forEach(elem => elem.classList.remove("focused"));
        /*event.target.classList.add("focused");*/
        this.classList.add('focused');
    }
}
}

CSS:

.button .focused{
background-color: darkgreen;
}

After clicking the button, the color does not change as expected. What changes should be made to correct this issue?

Answer №1

Your CSS code specifies to target the .focused class within the .button, which is why the color isn't changing. Removing the space between the two classes will resolve this issue.

window.onload = function(){
let buttonsX = document.getElementsByName("button_x");
for (let i = 0; i < buttonsX.length; i++) {
    buttonsX[i].onclick = function (event) {
        document.getElementById("hidden_x").value = event.target.value;
        buttonsX.forEach(elem => elem.classList.remove("focused"));
        this.classList.add('focused');
    }
}
}
.button.focused{
background-color: darkgreen;
}
<label class="instructions" for="hidden_x"> Insert X: </label>
<button type="button" class="button" name="button_x" value="1" id="x_1" onclick="document.getElementById('hidden_x').value = 1"> 1 </button>
<button type="button" class="button" name="button_x" value="2" id="x_2" onclick="document.getElementById('hidden_x').value = 2"> 2 </button>
<button type="button" class="button" name="button_x" value="3" id="x_3" onclick="document.getElementById('hidden_x').value = 3"> 3 </button>
<button type="button" class="button" name="button_x" value="4" id="x_4" onclick="document.getElementById('hidden_x').value = 4"> 4 </button>
<input type="hidden" name="hidden_x" id="hidden_x" value=""> <br>

Answer №2

Easy technique.... onclick="document.getElementById("sent Id of the button").

<!DOCTYPE html>
<html>
<body>
<style>
.mystyle {
  width: 20%;
  background-color: darkgreen;
  color: white;
  font-size: 25px;
}
.mystyle1 {
  width: 20%;
  color: white;
  background-color: grey;
  font-size: 25px;
}
</style>
<button class="mystyle1" id="x_1" onblur="myFunction(id)" onfocus="myFunction(id,'focus')">Click here</button>
<button class="mystyle1" id="x_2"onblur="myFunction(id)" onfocus="myFunction(id,'focus')">Click there</button>

<script>
function myFunction(ou,type) {
var element = document.getElementById(ou);
  if(type=="focus"){
   element.classList.add("mystyle");
    element.classList.remove("mystyle1");
}
else{
    element.classList.add("mystyle1");
    element.classList.remove("mystyle");
    }
}
</script>

</body>
</html>

Generate dynamic effects...

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

A guide on using Xpath to compare content between two tags

I am facing the following scenarios with $html contents. I am trying to determine if the html content starts with a media element (such as an image, video, or iframe) without any text content, similar to the third scenario below. //no contetn between firs ...

Performing an AJAX request to validate a username when it loses

Hey everyone, I'm currently working on a script that checks the availability of a username in a MySQL database using an onblur event with AJAX. Here's the AJAX script: document.getElementById("r_username").onblur = function(){ var http ...

Extremely sluggish pagination in JQGrid following the implementation of a filter through the filter toolbar

I've encountered a problem while using jqGrid with LOAD ONCE and client-side paging. The addition of a filter toolbar has significantly slowed down the paging process after applying any kind of filter. $(gridElement).jqGrid({ postData: post, ...

Give drawn elements a touch of fuzziness and experiment with the color of the fragment shader

I am currently experimenting with creating an animated gradient effect by blending three separate blobs together in a melting-like fashion, with each blob moving independently. The desired result can be seen in the image provided below. So far, I have bee ...

Blending conditional and non-conditional styles in VueJS

Is it possible to set a class in Vue based on the value of a parameter and conditionally add another class if that parameter meets a certain condition? Can these two functionalities be combined into one class assignment? <button :class="'btn btn-p ...

Tips for troubleshooting syntax errors in JavaScript

Are there any online tools similar to jsfiddle where I can input my JavaScript code and receive feedback on syntax errors, misplaced commas, or semicolons? Here is the example code I am looking to validate: $("#customer").autocomplete({ ...

The data within my <ui:define name="content"> element is not displaying on the JSF view

I'm currently working on a project that involves JSF/Facelets and a Style Sheet for styling my JSF view. I'm trying to add some graphical components like "h:inputText" and "h:commandButton" tags to my view XHTML, but for some reason, they are not ...

Angular service providing a subset of resource data

I am currently working with an Angular factory called DogePrice: .factory('DogePrice', ['$resource', function ($resource) { return $resource("https://chain.so/api/v2/get_info/DOGE"); }]) Usually, the API response looks like this: ...

Using React Hooks and useRef to Retrieve the clientHeight of Dynamically Rendered Images

I'm currently in the process of creating a dynamic image grid and need to determine the exact height of each image in pixels so I can adjust the layout accordingly. However, I've encountered an issue with accessing ref.current.clientHeight as it ...

Problem with Java class in GWT JsInterop

Having some trouble with JsInterop while wrapping up a piece of JavaScript code. The JavaScript code looks like this: com = { gwidgets: {} }; com.gwidgets.Spring = function () { this.name = "hello"; }; com.gwidgets.Spring.prototype.getName = ...

What is the method for obtaining receipt numbers in sequential order, such as the format AB0810001?

Is AB the receipt code that should remain constant followed by the current date (08) and 10001 as the receipt number? ...

Implementing Mouse Scroll Click Functionality in ASP.NET with C# (Without JavaScript)

How can I add a Mouse Scroll Click (Middle Button) event in ASP.NET and C#? I have already looked into the MouseWheel Event, but it did not provide the solution I was looking for. This is the order in which mouse events occur: MouseEnter MouseMo ...

Is it necessary for TypeScript classes that are intended for use by other classes to be explicitly exported and imported?

Is it necessary to explicitly export and import all classes intended for use by other classes? After upgrading my project from Angular 8 to Angular 10, I encountered errors that were not present before. These issues may be attributed to poor design or a m ...

Adding a command to open a new browser tab using JavaScript code can easily be accomplished by following these steps. By using Terminal, you can quickly and efficiently implement this feature

I'm new to coding and trying to create a terminal simulation. You can check out my code here: https://codepen.io/isdampe/pen/YpgOYr. Command: var coreCmds = { "clear": clear }; Answer (to clear the screen): function clear(argv, argc ...

What is the best way to use jQuery to update the color of an SVG shape?

Hello everyone! I'm excited to be a part of this community and looking forward to contributing in the future. But first, I could really use some assistance with what seems like a simple task! My focus has always been on web design, particularly HTML ...

Multiple columns contained within a span

I'm trying to create a panel that displays a list of values with corresponding labels. The requirement is for each label to be positioned above its respective value and aligned to the left. The layout should resemble the following: Label 1 Lab ...

Using Angular JS to connect Promises while preserving data

There have been discussions about chaining promises, but this scenario presents a unique challenge. I am currently working on making multiple http get requests in my code. The initial call returns an array, and for each object in this array, another http c ...

Verify whether a web application is equipped with React, Angular, or Vue

Is there an easy way to identify the client-side libraries used by an application if they are minified or compressed? While examining all the JavaScript sent from the server to the client, it can be challenging to determine if one of the top three popular ...

What is the best way to ensure that all inner divs occupy the full height of their parent div?

I am seeking a solution to make all child divs fill the full height of the parent div without specifying a fixed height. Ideally, I would like them to adjust to the height of the tallest child div. However, if the only way to achieve this is through JS, I ...

React Native Material - Implementing a loading indicator upon button press

With React Native Material, I am trying to implement a loading feature when a button is clicked. The goal is to show the "loading" message only when the button is active, and hide it otherwise. Additionally, I would like for the loading message to disappea ...