Changing the background color of a selected div using jQuery

I am working on a selection menu that includes a smarty foreach loop to display game venues, dates, and logos. When a user taps on a specific game, I want to change the background color of that div to green.

<div class="leftgame-container">
    {foreach from=$game_arrayleft item=row}
    <div class="left-column">
        <div class="team-logo">
            <img width="72" height="68" src="../public/img/logo/{$row.logo}" alt="Teamlogo">
        </div>
        <div class="team-name">
            {$row.venue}<br>
            {$row.game_date|date_format:"%e. %B  %Y"}    
        </div>
        <div class="team-check">
            {if $row.status eq 1}
            {html_checkboxes name='gamecheck' values=$row.id separator='<br />'}
            {/if}
        </div>
    </div>
    {/foreach}
</div>

After implementing a jQuery function to add the "greenBackground" class when a user clicks on a hyperlink, I encountered an issue where the background color persists when moving to the next game div. How can I remove this added class upon selecting a new game? Your assistance is greatly appreciated. Thank you.

Answer №1

Try out this code snippet:

$(document).ready(function () {
    $('a.pagerlink').click(function (event) {

        // Remove any existing 'greenBackground' classes
        $('.greenBackground').removeClass('greenBackground');

        console.log('hyperlink click');
        $(this).children('.manage-friends-first').addClass("greenBackground");
        var id = $(this).attr('id');
        $('#load').show();
        $.post("invited-friends",
                {gameid: id},
        function (data) { }).fail(function () { });
    });
});

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

What is the best way to ensure that my header remains responsive across all devices and screen sizes?

The header is not responding despite inputting width: 100%. How can I ensure that the header is responsive on all browsers, as well as iPads and phones? .head-wrap { background: url(http://envisionmediallc.com/prodigy/wp-content/uploads/2014/02/We-are-p ...

Transferring time for streaming MP3 files from server to HTML5 audio

Currently, my node.js server is set up to convert and stream mp3 files in real-time. I have integrated an HTML5 audio tag to play this stream, but I am encountering a problem - the audio element is unable to determine the duration of the mp3 until it has c ...

Finding the index of a chosen option in Angular

Attempting to retrieve the index of the selected option from a select element using Angular. The Angular (4) and Ionic 3 frameworks are being utilized. The template structure is as follows: <ion-select [(ngModel)]="obj.city"> <ion-option ...

Creating custom functions within views using Sencha Touch 2

I'm having trouble creating my own function in Sencha Touch 2 and I keep receiving an error: Uncaught ReferenceError: function22 is not defined The issue seems to be coming from my Position.js file located in the View directory. Ext.define(' ...

Disregard the instructions upon loading the page

I've implemented a directive to automatically focus on the newest input field added to my page. It works well, but there is one issue - when the page loads, the last input is selected by default. I want to exclude this behavior during page load and in ...

What is the best way to merge two tables together using the server-side JQuery Datatable plugin?

I recently came across an amazing example of a server-side ColdFusion jQuery datatable on this website: Check it out here However, I am facing an issue with adding a second table in the lookup. Specifically, while the primary table lists location_id, I al ...

Reactjs does not display a div element on the page

As part of a Laravel 8 project, I am utilizing ReactJS. When attempting to display the following component: <main className="main-container"> <section id="content-block" className="slider_area"> <div classNa ...

Inject the contents of an HTML file into a Vue div

Currently, I am utilizing jsfiddle to go through THIS {{respondedText}} <div>{{respondedText}}</div> But let's say I want to import HTML content from a file or website and then place it inside that div instead of showing "Event Rec ...

Exploring the functionalities of the modulus operator in Handlebars

I am looking to implement a solution that involves adding a div container holding 4 images based on the JSON data provided below: var pictures = [ {img_path: "1/1.jpg"}, {img_path: "1/2.jpg"}, {img_path: "1/3.jpg"}, {img_path: "1/4 ...

Using AJAX to search through paginated pages is quite simple and effective

Currently, I have developed a JavaScript script that filters names within a list. However, a problem has arisen with pagination - as the list paginates, I am unable to retrieve names without refreshing the page. Is there a way to use AJAX to access and dis ...

generate a series of nested divs within one another

I am looking to create a custom nested loop that will generate div elements based on the content of my h1 and h2/h3 tags. I understand this may have been covered in other inquiries, so any guidance would be appreciated :) Here is the initial HTML: <h1& ...

Upgrade angular-chart.js when applied to a filtered collection

I recently started working with Angular and encountered an issue while incorporating Angular Charts JS. I have a list that can be filtered using search text, and I want my charts to update whenever the list is filtered. What would be the best approach to ...

Javascript recursive method for fetching data entries

Seeking a solution to retrieve interconnected records based on a parent column, where the relation can be one or many on both ends. After attempting a recursive function without success, I found my code became overly complex and ineffective. Is there a st ...

Is there a way to prevent the React history.push from reloading the component every time? I only want

After loading a component and redirecting using history.push, my component renders twice, triggering the componentDidMount() function twice as well. Below is a simplified version of my component. Everything runs fine with componentDidMount() running only ...

Guidelines for capturing a webpage screenshot and sending it back to the client using asp.net and phantomjs

My goal is to read a screenshot of a website using phantomjs and ASP.NET. Initially, I attempted to save the screenshot to a file using page.render. This method worked smoothly in a console application but faced issues when called from an ASP.NET handler, ...

When HTML files are uploaded to a server, the icons may not appear on the website

I've noticed that the icons on my website only seem to display when I click on my HTML files, but not when the files are hosted on my server. Here is the link to my website where the icons are not displaying properly. ...

Guide to selecting HTML components from an Angular service

Within my app.component, I have integrated a component called comp1. The HTML file for comp1.component.html contains buttons defined as follows: <button #myButton (click)='function()'> Temporary </button> <button #myButton2 (click ...

Modifying input field attributes with JavaScript

function updateStationeryType() { document.querySelector('[name="stationerytype[]"]').value = "" var purpose = document.getElementById('purpose').value; if (purpose === "Meeting") { v ...

Using the IE method getelementbyid to target an object within the document

Is there a way to use getElementById to access an object that already exists in the document? I am specifically trying to target the element "test" which is nested within parentDiv1. While this code works in Firefox, it's not functioning properly ...

Using PHP's header function to alter directories

So I am currently diving into PHP and facing a challenge with using headers. My setup involves using xampp for development purposes. I have a basic form where users can log in on "index.php". Upon successful login, the header function kicks in to redirect ...