Showing a cell border when a radio button is chosen

I am working with a table that contains input/radio buttons, and I am trying to figure out how to apply a border to a specific cell when the user selects the radio button within it. This will give the appearance of the cell being selected. I am not sure how to accomplish this task. Below is an example of the table:

<tr class="crna">
  <td>Crna</td>
  <td class="no-value"></td>
  <td>
    <input id="r-band2-crna" value="0" type="radio" name="prsten-2-radio" checked="checked">
    <label for="r-band2-crna">0</label>
  </td>
  <td>
    <input id="r-band3-crna" value="0" type="radio" name="prsten-3-radio" checked="checked"> 
    <label for="r-band3-crna">0</label>
  </td>
  <td>
    <input id="r-multi-crna" value="1" type="radio" name="prsten-4-radio" checked="checked"> 
    <label for="r-multi-crna">x10<sup>0</sup></label>
  </td>
  <td class="no-value"></td>
  <td><input id="r-tcr-crna" value="250" type="radio" name="tcr-radio" checked="checked">
     <label for="r-tcr-crna">±250</label>
  </td>
</tr>

I need to implement this using jQuery and CSS. Any guidance on how to achieve this would be greatly appreciated.

Answer №1

This solution involves utilizing jQuery along with CSS.

The border is applied using the addClass() method. The corresponding CSS class must be defined as follows:

.current {
  border: 1px solid green;
}

Additionally, the closest() method is used to target the specified parent element of the <td> tag.

$('input[type="radio"]').on('change', function() {
  $('input[type="radio"]').closest('td.current').removeClass('current');
  $(this).closest('td').addClass('current');
});
.current {
  border: 1px solid green;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tr class="crna">
    <td>Crna</td>
    <td class="no-value"></td>
    <td>
      <input id="r-band2-crna" value="0" type="radio" name="prsten">
      <label for="r-band2-crna">0</label>
    </td>
    <td>
      <input id="r-band3-crna" value="0" type="radio" name="prsten"> 
      <label for="r-band3-crna">0</label>
    </td>
    <td>
      <input id="r-multi-crna" value="1" type="radio" name="prsten"> 
      <label for="r-multi-crna">x10<sup>0</sup></label>
    </td>
    <td class="no-value"></td>
    <td><input id="r-tcr-crna" value="250" type="radio" name="prsten">
       <label for="r-tcr-crna">±250</label>
    </td>
  </tr>
</table>

Answer №2

If you're looking to get started in the right direction, here are some key concepts to consider:

  1. One way to apply a style when a radio button is toggled is by utilizing events like click and change in JavaScript or using the :checked CSS pseudo-class.

  2. You can add or remove a class attribute (such as 'active-cell') on the event to represent a border around the element. This may require some creativity in styling due to the complexities of applying borders to table cells.

Adding and removing classes dynamically can simplify your HTML and CSS code, while using pseudo-classes in CSS eliminates the need for JavaScript. However, styling the parent element of the input requires strategic selector usage and potentially adding positioning properties.

input[type="radio"]::checked + label::before { 
  /* styles will be applied to labels following selected radio inputs */
}

Answer №3

Make sure that all radio buttons within the same group have the same name to function properly. Additionally, why are all buttons checked by default?

To achieve your desired outcome, you can utilize event listeners that respond to specific actions and modify the Document Object Model (DOM) accordingly.

For instance, with radio buttons, you can implement an event listener for the "change" action.

    $(document).ready(function(){
        $('input[name="prsten-2-radio"]').change(function(){
            
            // Remove border from other cells
            $('input[name="prsten-2-radio"]').parent().attr('style', 'none')
            
            // Apply a border only to the selected cell
            $(this).parent().attr('style', 'border: solid black')
            
            
        })
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
        <tr class="crna">
            <td>Crna</td>
            <td class="no-value"></td>
            <td>
              <input id="r-band2-crna" value="0" type="radio" name="prsten-2-radio">
              <label for="r-band2-crna">0</label>
            </td>
            <td>
              <input id="r-band3-crna" value="0" type="radio" name="prsten-2-radio"> 
              <label for="r-band3-crna">0</label>
            </td>
            <td>
              <input id="r-multi-crna" value="1" type="radio" name="prsten-2-radio"> 
              <label for="r-multi-crna">x10<sup>0</sup></label>
            </td>
            <td class="no-value"></td>
            <td><input id="r-tcr-crna" value="250" type="radio" name="prsten-2-radio">
               <label for="r-tcr-crna">±250</label>
            </td>
          </tr>
    </table>

Check out these helpful resources:

Best of luck!

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

How can I safeguard my HTML and CSS content from being altered using tools similar to Firebug?

Is there a method to deter the alteration of HTML and CSS components on a webpage using tools similar to Firebug? I have noticed that certain users are changing values in hidden fields and modifying content embedded within div or span tags for their perso ...

The active class consistently adds new menu options without removing the previous ones

I am facing an issue with my menu where the active class does not get added to the new menu item when clicked, despite being removed from the previous item. Below is the code snippet: <div class="row text-center" id="side-menu"> <nav> ...

Turn off smooth scrolling in Bootstrap 5 using HTML and CSS techniques

On my website, I have implemented anchor links that activate smooth scrolling when clicked, thanks to the Bootstrap feature. However, the unique layout of my website requires me to turn off smooth scrolling in either the HTML or CSS code. I appreciate an ...

Utilizing jQuery to place an element beside another and maintain its position

I need ElementA to be positioned next to ElementB as shown in this example. The key difference is that I want ElementA to move along with ElementB if the latter is relocated. Is there a way to keep a specific element fixed to another one? Re-calculating ...

The boundary extends fully to the right

Recently, I created a calculator to convert kilograms to pounds and encountered an issue when trying to add a border to a p element. The problem was that the border extended all the way to the right side of the page. Here is the corresponding HTML code: &l ...

ControlsContainer - jQuery object for Flexslider

I recently tried to implement a flexslider with controls in a separate div using the controlsContainer option provided by flexslider. The setup consists of a left column containing the slider code (".flexslider-small"), a right column with text, and the c ...

Using JavaScript to Toggle Visibility of Div Elements

Hi there! I'm having some trouble with a code that is supposed to show/hide div content based on the selection from a jump menu. Unfortunately, it's not working as expected. Here is the snippet of my code: JS: function toggleOther(chosen){ if ( ...

PHP button echo not working as expected

Utilizing jquery ajax, I am fetching data from b.php which includes a button. echo '<button id="btn" value="nice!">Click!</button>'; $.ajax({ type: "GET", url: "b.php", dataType: "html", success: function(data){ ...

Unsupported MIME format

I'm encountering an issue where my stylesheet is not applying to my handlebars. It was working fine yesterday, but today I'm seeing a MIME error and I'm unsure of the reason behind it. Any assistance would be greatly appreciated. Server Cod ...

Error message: "An undefined index error occurred during an Ajax call to a

Path: homepage -> initiate ajax request to tester.php on PHP -> receive JSON data back to homepage. I am struggling to resolve this issue. Any help would be appreciated. AJAX Request: $.ajax({ url : "tester.php", ty ...

Can the color of text be adjusted (to either white or black) based on the background color (in any color and format)?

To achieve a text color that contrasts well with any background, I need to make sure it's either black or white. The background in my app can vary in color and format, so finding the perfect solution has been challenging. Using mix-blend-mode doesn&a ...

Using AJAX to retrieve Laravel Route results in incorrect URL leading to 404 Error (Not Found)

I am experiencing an issue with my route setup in web.php. Here is the route definition: Route::delete('/contributions/contribution/destroy', 'ContributionController@destroy'); In my blade file, I have a button that triggers a sweetal ...

Maintain line breaks in the scanner input within an HTML element

Currently, I am utilizing a Zebra DS9908 scanner to scan a barcode and transfer the data onto an HTML page. However, I am encountering an issue with preserving all input characters. Despite attempting both a <div> and <textarea>, the line feed ...

What's the best way to update the fill color of an SVG dynamically?

Is it possible to change the color of an SVG image dynamically without using inline SVG tags? I want to create a code that allows users to specify the source for the SVG tag and a hexadecimal color value, enabling them to customize any SVG image with their ...

Convert JavaScript code to Google Appscript

Looking for guidance on incorporating Javascript code into my Google Appscript. Here is the code snippet: I have a separate Stylesheet.html file saved. Can someone advise me on how to invoke the functions within the html file? <script> //google. ...

The controller method in Laravel is not being triggered by AJAX

In my blade layout file, I have some code that is included in my view multiple times using the @include.layout("blade file link") syntax. When a button is pressed, I want to call a controller function. This works fine without AJAX, but when AJAX is added, ...

Is the XPath in Selenium executed against the Document Object Model (DOM) or the HTML file

Currently, I am in the process of developing a library for analyzing webpages. My approach involves using Selenium to access elements on a webpage through xpath. I have been contemplating replacing Selenium with an offline xpath tool. However, upon furthe ...

Finding the class name of the parent div: A simple guide

Within a maze of nested divs, there lies a checkbox waiting to be deciphered. When the OnChange event triggers, I aim to unveil the hidden class name of the parent div/container div known as pan-box placeholder. The HTML journey begins with this structure ...

Tips on utilizing CSS modules in React without changing class names

After starting to use css modules in my react project, I quickly realized the struggle of changing classnames to fit the requirements of css modules. For example, if we have a component using regular css: import React from 'react' import ". ...

The Php Include function is known to create unnecessary whitespace and disregard any specified styles

On one of my web pages, I am using the include() function to bring in content from another page. This external page contains both HTML and PHP code, with echoes for output. Here's an example: The page Apple.php looks like this: <html> <head ...