Manipulating several elements' style using JavaScript when clicked

Can you guess who will win a particular sports match based on the images below?

Here's what I'm trying to achieve:
  • Change the text color of selected teams to black so that users can easily see their selection before submitting
while($fixtures > $upcoming){
    // code for selecting team and score
}

// When team names are displayed, change style on click
<div id="dispPicks">
    foreach($dispTeam1 as $key => $team1) {
        echo '<p class="team1">' . $team1 . '</p>';
        echo ' VS ';
        echo '<p class="team2">' . $dispTeam2[$key] . '</p>';
   }
</div>

I attempted to write this JavaScript:

var elements = document.getElementById("makePicks").elements;
var len = elements.length;
var team = [];
for(x=0; x<len; x++){
    if(elements[x].type == "radio" && elements[x].checked== true) {
        team[x] = t1[x].value;
    }
}

My issue:

The variable team[x] now holds the selected team, but I am unsure how to proceed with changing the style of the team names from here.

Considerations:

  • The teams/fixtures are dynamically generated from a PHP loop in every round, resulting in a different number of fixtures each time.

Answer №1

If you want to ensure consistency, assign the same class to all clickable teams - for example, class="team". Update your CSS with the following:

.selected{
   background-color:#fff;
}

Next, incorporate JQuery in this manner:

$('.team').click(function(){
   $(this).toggleClass('selected');
})

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

AngularJS directive and customized markup/function

Here is the code snippet I am currently working with: <body ng-controller="testController"> <div test-directive transform="transform()"> </div> <script type="text/ng-template" id="testDirective.html"> <div& ...

When combining Symfony, Twig, and Semantic UI, users may encounter an issue where the background image fails to display

Can someone assist me in identifying the issue with my code? I am using Symfony 2.8, Semantic UI, and Twig, attempting to set a background image on my site. The modification is being made to my base.html.twig file. I have attempted: Using different ima ...

Having trouble creating a fully responsive carousel using Bootstrap v4-alpha

I am interested in creating a functional carousel similar to this one: However, when I upload my images and resize the carousel for small screens, I encounter an issue where part of the old image holder appears below the images like shown here: I simply ...

Testing Angular Translation - Struggling with injecting $translateProvider

I'm currently facing an issue with my unit test using ES6, AngularJS, Karma/Jasmine, and angular-translate. It seems that I'm unable to get my unit test to pass. I'm not very familiar with how to handle 3rd party modules, such as angular-tra ...

jQuery does not provide the reference of a basic canvas element

I'm having trouble with a simple initialization function that is supposed to create a canvas element in the body and save its reference in a variable. No matter what I try, jQuery doesn't seem to want to return the reference. I attempted refere ...

The vertical alignment in Bootstrap form grid layout can be frustrating to work with

Looking at the image, it's clear that there's an issue with my Bootstrap 5 form layout. One of the labels has multiple lines of text and causing it to be misaligned with the other elements in the grid. The problematic HTML code is as follows: ...

Which medium is the optimal choice for file manipulation in Node.js and JavaScript?

Is there a simple way for JavaScript to receive incoming data from Node.js that was obtained from an external server? I've been searching for a solution to this problem for days now. Here's my current approach: I plan to have both the server-sid ...

How to create HTML buttons with CSS that maintain proper proportions?

How can I ensure that my HTML buttons adjust proportionally to different screen sizes? I've been working on coding an Android app using HTML and CSS, and everything seems to be running smoothly. However, when I share the file with a friend, he compla ...

What are the steps for retrieving a JSON object within an array?

var jsonData = '[{"type":"product","id":1,"label":"Size","placeholder":"Select Size","description":"","defaultValue" :{"text":"Size30","price":"20"},"choices":[{"text":"Size30","price":"20","isSelected":"true"},{"text" :"Size32","price":"22","isSelec ...

Is there a way to choose a data file in an HTML/JavaScript document for inclusion?

I have created a program for generating graphs, but every time it is executed, the user needs to input a data file to plot. Below is a basic program with the data file (Data.js) hardcoded in line 7: <!DOCTYPE html> <html> <head> &l ...

Display a div when hovering over another div

I have a menu that looks like this: https://i.sstatic.net/WqN33.png and I want to create a hover effect where another div shows up over each item, similar to this: https://i.sstatic.net/JRaoF.png However, I can't seem to figure out how to implemen ...

The Reason Behind Angular Error when Using CSS Custom Tags

I have the following SCSS code: main{ width: 100%; height: 840px; /*background: red;*/ margin: 10px auto; position: relative; padding: 5px 0; } section{ width: 98%; height: 600px; margin: auto; display: flex; ...

Replace the CSS styling with new code from different files

After downloading a large web template containing numerous CSS and JS files, I have successfully created a visually appealing website. However, there are still some minor details that I wish to modify, such as the font style and colors. The issue arises w ...

Error encountered when submitting HTML form containing file input: ERR_CONNECTION_RESET

I'm encountering an issue while trying to upload an image using an HTML form. Once I submit the form, after approximately 3-5 seconds, I receive the following error on the browser: ERR_CONNECTION_RESET The problem seems to arise with images larger ...

Invoke a Vue component's function using a property

Can a Vue component's method be triggered by passing the method's name to one of its properties? // main.vue <navigation :button-left="goback()"></navigation> // navigation.component.vue ... props: ["buttonLeft" ...

An HTML form containing various input fields organized as a JSON string

I need to pass two parameters, param1 and param2, to a servlet. Param1 should be in JSON format, while param2 is a regular input value. param1 = {"id":"userid","name":"username","status":"userstatus"} param2 = add How can I accomplish this using the HTM ...

NodeJs Tutorial: Deleting an HTML Element Without a Tag

I encountered a situation where I had an HTML string like this: <div> <p>a</p> <p>b</p> <p>c</p> <img src='a.jpg'> <img src='b.jpg'> d e f </div> Afte ...

Retrieve the ID Element from a separate HTML document

After countless hours of searching online, I still haven't found a solution... My goal is to integrate elements from another HTML file into my own homepage using AngularJS. I'm hopeful that someone here can provide assistance. Thank you! ...

Automated updating feature with jQuery

Is there a way to use jQuery code to automatically refresh a page after navigating back in the browser history? Here is an example of what I have tried: jQuery(".form_" + form_count).html("<div id='message'></div>") jQuery('#me ...

React: Issue encountered when updating props from parent component (Error executing removeChild operation)

Currently, I am utilizing react along with AJAX to retrieve data for a list. However, every time componentWillReceiveProps(nextProps) is triggered after the initial setup, an error occurs. This error typically arises when the URL for the AJAX call has chan ...