Modify the border color within a table upon selecting a radio button

Looking to update the border color of a table when a radio button is clicked.

Attempted to use jQuery, but not seeing the desired result.

Here is the code snippet: https://codepen.io/ervannnn/pen/gOObrdP

Javascript:

$('input[type=radio]').change(function() {    
    $('.box-tr-1').removeClass().addClass('box-tr-1-se');
    $(this).parent().addClass('box-tr-1');
});

CSS:

table {
    border-collapse: collapse;
}
.table-item {
    width: 90%;
}
.box-tr-1 {
    border-left: 4px solid #ddd;
    border-right: 4px solid #ddd;
    border-top: 4px solid #ddd;
}
.box-tr-2 {
    border-left: 4px solid #ddd;
    border-right: 4px solid #ddd;
}

.box-tr-3 {
    border-top: 4px solid #ddd;
}

.box-tr-1-se {
    border-left: 4px solid red;
    border-right: 4px solid red;
    border-top: 4px solid red;
}
.box-tr-2-se {
    border-left: 4px solid red;
    border-right: 4px solid red;
}

.box-tr-3-se {
    border-top: 4px solid red;
}

CodePen link: https://codepen.io/ervannnn/pen/gOObrdP

Any assistance would be greatly appreciated. Thank you!

Answer №1

Your code may be a bit messy, but it should serve its purpose:

$('input[type=radio]').change(function() {   
  $('tr').each(function(){
    $(this).removeClass('box-tr-1-se');
    $(this).removeClass('box-tr-2-se');
  })
  if($(this).is(':checked')){
    $(this).parent().parent().addClass('box-tr-1-se');
    $(this).parent().parent().next().addClass('box-tr-2-se')          
  }
});

.box-tr-2-se {
  border-left: 4px solid red;
  border-right: 4px solid red;
  border-bottom: 4px solid red;
 }

UPDATE: Adjusting text color:

$('.table-item input[type=radio]').change(function() {
  $('.table-item tr').each(function(){
    //Previous methods...
    $(this).find('.red').removeClass('red')
  })
  if($(this).is(':checked')){
    //Previous methods...
    $(this).parent().next().children().eq(1).addClass('red');
  }
});

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

Is there a way to store information in a kendo grid without relying on the KendoGrid toolbar?

For my testing purposes, I have created a grid setup like this: function TestCatalogDefaultsGrid() { $("#testCatalogDefaultGrid").kendoGrid({ columns: [{ field: "Catalog", template: '<select class="form-control" ...

How can we ensure that the borders of a text field automatically adjust to fit the dimensions of the text field using CSS?

I've encountered an issue with the borders of text fields while working on a project involving CSS/HTML. The problem is that in the browser, the borders appear shorter than the text fields. Initially, I attempted to adjust the border size to match th ...

Implement modals using a for loop in Vue

I am facing an issue while trying to create modals for cards within loops. I attempted using v-bind:id="masa._id" and v-bind:data-target="'#'+masa._id", but the modal is not working. Can someone guide me on how to implement modals in loops? Belo ...

Simulating a winston logger in jest testing

I am pondering how to create mock transports for the File module within the Winston node package. While utilizing Jest, the __mocks__/winston.ts file is automatically loaded. My dilemma lies in the fact that I am unable to mock it due to the presence of th ...

Having trouble accessing the length property of an undefined array while passing it from Node to Jade

Hey there! I'm currently working on rendering jade from node using the following code: router.get("/register", function (req, res) { var countries = [1, 2, 3]; res.render("./account/register", { countries: countries }); }); Below ...

I'm puzzled by the "Uncaught TypeError:" error message and can't seem to figure it out

I'm having trouble displaying the value of 'true' within my objects and keep encountering an error message. Uncaught TypeError: Cannot set property 'toDoIsRemoved' of undefined at removeTask (main.js:85) at HTMLButtonElemen ...

I need to use HTML formatting to ensure that my form appears professional and visually pleasing

Currently, I am working on enhancing the appearance of an HTML form. However, I seem to be struggling with the formatting, and I also aim to ensure that the form is responsive. My goal is to have the first name and last name fields on one line, followed by ...

AngularJS: Extending HTML templates through inheritance

Query: How can I avoid redundancy in similar HTML templates? About: I have multiple templates for the same view/directive that need to be adjusted based on the environment. Although most of the templates are similar, there are certain sections that vary ...

Filter your data in jQuery DataTables using individual filters for each column

I'm currently working on a DataTables table that pulls data from an AJAX source. I have successfully set up the table and made searching functional. However, I've been asked to add search fields for each column. I found a DataTables Plugin for c ...

What could be causing the JSF ajax status success to fail in Chrome?

Whenever I trigger an action in my ManagedBean, I also initiate a JavaScript function via JSF ajax to display a loading popup while the action is being processed. This functionality works flawlessly in Firefox, however, it seems to encounter issues in Chro ...

The hyperlink associated with the image is not functioning

We are currently in the process of building a website, but we seem to be encountering some issues with the links. Specifically, when navigating away from the homepage and clicking on the logo in the corner, we should ideally be directed back to the index ...

image animation in jquery not functioning properly

I'm attempting to create an image that fades in from the left upon loading until it reaches a certain point on the screen. Despite thinking my code is correct, nothing is happening. Can someone please assist me with this issue? Here is the function I ...

Django issue: A Tuple or struct_time argument is necessary

Currently, I am developing a code that deals with 2 variables - showdate and viewtype. Both of these variables are transferred via JavaScript using the POST method. viewtype = send an srt showdate = send a date from javascript Within this code snippet, ...

What can I do to stop my array from continuously updating in the onChange function in React JS?

I duplicated an array and implemented an onChange function. However, when I call the onChange function, both my original and duplicated arrays are being updated based on the new value from onChange. I specifically do not want my duplicated array to be affe ...

AJAX has caused the webpage to freeze, prompting a warning about potentially slowing down the browser

My website features an AJAX call to the Alpha Vantage API, which retrieves JSON data on stock symbols and tickers as users type in the search box. function handleKeyPress() { var keywords = document.getElementById("TextBox89 ...

Tick the checkbox if the Angular function evaluates to true

I'm facing a challenge with checkboxes related to available cars and determining whether they need to be checked based on the user's rented cars. Essentially, I have an array of all available cars named $scope.cars, and each user has an array ca ...

Whenever I press the view button, all the cards open instead of just the one I chose from the index. Can anyone tell me where I am going wrong?

I've encountered an issue with the View buttons I added to display more detailed information about the characters in the card bodies. When I click on the view button, it opens all the boxes instead of the one I selected. I've tried using a for lo ...

A comprehensive guide on using Lua Script in Node.js to efficiently insert multiple records into a Redis Hash

How can I efficiently insert multiple records into a Redis Hash using Lua Script in Node.js? I currently have the following code which utilizes multi and exec for inserting data. How can I modify it to use a lua script instead? return new Promise ...

Functions perfectly on jsfiddle, but encounters issues on Dreamweaver

After reading that I needed to add back the event handler for my code to work in Dreamweaver from JS Fiddle, I made sure it was included but still can't get it to function. The Demo can be found here: http://jsfiddle.net/EfLJJ/6/ Below is my JavaScri ...

Looping through the ajax data to populate ion-item elements

I am currently working on a loop that retrieves user IDs, names, etc. from a JSON file. I am trying to display this data in a list view within an Ionic framework. When I simply use direct alerts in JavaScript, the users are displayed correctly, but when I ...