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

What is the best way to align inline-block elements in a straight row?

I am encountering an issue with the code block below: <div class="1"> Foo<br>1 </div> <div class="2"> Bar<br>2 </div> Even though both .1 and .2 have styles of position:relative;display:inline-block, they are displ ...

Ever since I switched to a different monitor, my Javascript has suddenly stopped functioning

I'm encountering an issue where my JS stops working every time I switch displays within a single HTML file. I've attempted to replace the HTML onclick call with a JavaScript function, but the problem persists. Update: Apologies for the unclear e ...

Angular: Unable to retrieve defined data when loading a component

There is a nagging question in my mind that I hesitate to ask because deep down, I know the answer is probably staring me in the face. After struggling with code for two days straight, I am on the brink of pulling my hair out. Although I am relatively new ...

I am looking to implement user authentication using firebase, however, the sign-in page is currently allowing invalid inputs

<script> function save_user() { const uid = document.getElementById('user_id'); const userpwd = document.getElementById('user_pwd'); const btnregister=document.getElementById('btn-acti ...

Execute a PHP script upon button click without the need to refresh the page

I'm facing an issue with integrating PHP and JavaScript. Objective: To execute a .php script when the event listener of the HTML button in the .js file is triggered without causing the page to reload. Expected outcome: On clicking the button, the PH ...

Getting a pair of `focusin` events upon focusing

My Current Project As I work on my project, I encountered an interesting issue with event listeners and focus handling in different browsers. Specifically, when using jQuery's .on() to register a listener for the focusin event and programmatically se ...

Is it possible to transmit an image using ajax and php without using a form or input type file?

Seeking a unique solution here. I am interested in enabling a straightforward image upload directly from a mobile camera. No need for a form, just display the image in a preview and allow the user to click to upload it to a PHP script for processing. I&ap ...

showcase every value upon submission in the form with options to edit and delete

Is it possible to display all values submitted in a form with edit and delete buttons? Currently, only one value is being displayed at a time. Whenever a new value is displayed, it replaces the old one. How can this be fixed? You can fin ...

Unable to apply text decoration to a floating element

Why is text-decoration not applying to a span inside a div after floating the span, and how can this be fixed? To see the issue in action, visit: http://jsfiddle.net/wtBDX/2/ div { color: red; text-decoration: line-through; } div span { float: rig ...

Only apply the CSS filter alpha to the parent element and not its children

I am working on implementing an alert message box using HTML and CSS. Below is the code I have so far: <div class="dim" id="msg"> <div class="dialog_wrapper"> <div class="dialog" id="msg_value"></div> ...

Exploring the concept: Uploading images simultaneously on Facebook - Unraveling the

I am currently working on implementing an ajax-style submission form for File Upload. After successfully getting a progress bar updated, it seems like the whole logic is working fine. However, I recently observed Facebook and noticed that: 1) Facebook a ...

Using $.getJSON is not functioning properly, but including the JSON object directly within the script is effective

I'm currently working on dynamically creating a simple select element where an object's property serves as the option, based on specific constraints. Everything is functioning properly when my JSON data is part of the script. FIDDLE The follow ...

Using both jQuery and Ajax can result in multiple requests being sent when utilizing the focus

Hey there, I've got a form with around 20 input fields. My goal is to trigger an AJAX request every time a user moves from one field to another. Everything seems to be working fine, but there's a peculiar issue. Upon moving from one field to th ...

Is there a way to adjust the height of a DIV based on the background image and keep the text centered at the same time?

Currently in the process of designing a landing page for my website, and I am interested in incorporating a Jumbotron to provide a more modern and tech-savvy touch. Successfully managed to center my text both horizontally and vertically, now looking into s ...

Learn how to efficiently redirect users without losing any valuable data after they sign up using localStorage

I am currently facing an issue with my sign up form. Whenever a user creates an account, I use localStorage to save the form values. However, if the user is redirected to another page after hitting the submit button, only the last user's data is saved ...

How can I retrieve the URL of images listed in my CSS file using Python?

I need help extracting all image URLs used in a CSS file. Here is an example snippet from my CSS file: sitewrap { width: 890px; margin: 0 auto; background: #ffffff url(../images/bg_sitewrap.gif) repeat-y; } Can anyone advise on how to extract ...

Understanding the relationship between CSS height and line-height can help developers create more

Is there a way to use CSS to set the height of a box based on its own line-height or its parent's line-height? This feature would make it much simpler to create text areas that are a specific multiple of lines, for example, displaying exactly three l ...

Winning opportunities created by using credits in the slot machine

**Greetings, I have created a slot machine using JavaScript, but I am looking to enhance the player's chances based on their credits. Can anyone guide me on how to achieve this? Essentially, I want the player's odds to increase proportionally wit ...

Is it possible to use jQuery/JS to automatically format currency input as it is typed, adding a 1000 separator and ensuring

I'm working on a text input field that needs to format the value as it is being typed, with restrictions of 2 decimal places and 1000 separators. This field should only allow for digits to be entered. Specifically, this input is meant for users to ent ...

Let's discuss how to include the scrollTop option

I am new to coding and I need help adding a scrollTop margin of +100px to my code. The page already has some top margin but I can't seem to locate it. I'm also having trouble finding where the margin-top is being set in the JavaScript file. /*** ...