Interested in implementing Bind and Unbind functionality to enable hovering over a different row after selecting a radio button

Can someone assist me in incorporating the Bind and Unbind functions? I have a table with multiple rows, each containing radio buttons. Currently, when I hover over a row it changes to light blue. However, once I select a radio button, I can no longer hover over the other rows. How can I fix this issue?

<table id ="myTable">
 <tr>
<th>Selected</th>
<th>First Name</th>
<th>Middle Name</th>
<th style="width: 10%;">Last Name</th>
<th>Active Email</th>

       <tr>
    <td><input class ="rad" type="radio" name="developerRadio" value="Value 1" /></td>
    <td>Hello</td>
    <td>everyone</td>
    <td>good</td>
    <td>email</td>
    <td>hello</td>

 </tr>
           <tr>
    <td><input class ="rad" type="radio" name="developerRadio" value="Value 1" /></td>
    <td>Hello</td>
    <td>everyone</td>
    <td>good</td>
    <td>email</td>
    <td>hello</td>

 </tr>
        <tr>
    <td><input class ="rad" type="radio" name="developerRadio" value="Value 1" /></td>
    <td>Hello</td>
    <td>everyone</td>
    <td>good</td>
    <td>email</td>
    <td>hello</td>

 </tr>




<script>
$(document).ready(function () {
    $("tr").mouseover(function () {
        $(this).css("background-color", "#0066FF");
    });
    $("tr").mouseout(function () {
        $(this).css("background-color", "");
    });

    $('input:radio').click(function () {
        $("tr").unbind("mouseover mouseout");
        $(this)
            .closest('tr')
                .siblings()
                .css('background-color', 'white')
                .end()
            .css('background-color', 'darkblue');
        $("input:radio")
            .unbind("click")
            .click(function () {
                $('tr').bind("mouseover");
                $(this).closest('tr').css('background-color', "#0066FF");
        });
    });
});
</script>

Answer №1

Avoid the need to bind and unbind events by simply using an active class for hover behavior:

$("table")
  .on("mouseenter mouseleave", "tr:not(.selected)", function(e){
    $(this).toggleClass("hovered", e.type === "mouseenter"); })
  .on("click", "input:radio", function(){
    $(this).closest("tr").addClass("selected"); });​

Check out this demonstration on http://jsfiddle.net/Bc2DN/1/

You can also simplify things by relying more on CSS:

<style>
  tr:hover { background: yellow }
  tr.selected { background: orange }
</style>

<script>
  $("input:radio").on("click", function(){
     $(this).closest("tr").addClass("selected"); 
  });
</script>

View the example in action here: http://jsfiddle.net/Bc2DN/2/

Answer №2

What do you think about using classic CSS for this?

tr {background: transparent;}
tr:hover {background: #0066FF;}
tr.active {background: lightgoldenrod;}
tr.active:hover {background: yellow;}

You can then apply an active class to the radio button's tr element like so:

$("input[type='radio']").click(function(
    $(this).parentsUntil("tr").parent().addClass("active")
    $(this).parentsUntil("tr").parent().siblings().removeClass("active");
));

Answer №3

Understanding the complexities of color manipulation and event handling in web development can be challenging without practical examples. Here is a potential solution:

$(document).ready(function () {
    $("tr").on({
        mouseover: function () {
            if (!$(this).find('input:radio').prop('checked')) {
                $(this).css("background-color", "#0066FF");
            }
        },
        mouseout: function () {
            $(this).css("background-color", "");
        }
    });

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

The 'FileNotFoundException' error was triggered due to the 'Copy local = false' setting

I have around 20 solutions, each with 5-10 projects that reference each other and some third-party libraries. All of these projects are built in the same Output folder, and the requirement is to not have any duplicate copies of each assembly being referenc ...

Exploring the integration of AJAX and jQuery with Django 1.3

I am completely new to the Django framework, web development, and Python. Currently, I am trying to incorporate AJAX into my project but struggling to find a working sample. I need assistance with integrating AJAX or jQuery within a Django 1.3 project. Cu ...

Eliminate table borders in a data grid using bslib and Bootstrap design

Within my shiny app, I am implementing the yeti theme using the bslib library along with a datatable that features bootstrap styling. However, I have encountered an issue where unwanted white cell borders are appearing in the datatable when compared to uti ...

Using parameters from jQuery to populate an ASP.NET MVC partial view

Currently, I am focusing on the Location Detector Module within the Project. To detect latitude and longitude using jQuery, there is a button that submits the ajax form to the action, which then returns a partial view. I am wondering how I can automatica ...

Enhancing Website Design with Multiple Themes in ASP.NET

I am incorporating an App_Themes directory into my ASP.NET website that also uses Master pages. Is it possible for me to utilize two distinct themes depending on the specific master page? ...

What steps can be taken to deactivate a cascading kendo combobox once it has finished loading?

Currently, I am working with two kendo comboboxes that are cascading from one another. Both of them load data without any issues. However, the problem arises when I try to disable the second combobox (comboSequenceNumbers) after a selection has been made. ...

Loop through the items in the List and retrieve various values linked to a dropdown menu for a specific item by utilizing both jquery and SharePoint web services

I am currently working on a project where I have a list and I am using caml query to select an item based on certain criteria. The item has multiple lookup fields, and my goal is to bind them to a dropdown list. However, the code I have written so far only ...

Transmitting the contents of $body in Mysql as table information, not as

Looking for a way to send MySQL database data via email using PHP Mailer. I have managed to send it as an attachment, but struggling to send it as a formatted body. When I try to use file_get_contents on 'displaywebpage.php', it only reads the HT ...

Storing items in the cart using localStorage and fetching them using AJAX in a Spring Boot application

I am currently developing an online shopping spring boot web application that includes an ADD TO CART functionality. Initially, I attempted to implement this feature using localStorage, but I encountered storage limitations where items cannot exceed 3 or 5 ...

Ignoring Bootstrap Popover Placement Setting

Within my Bootstrap modal, there is a select element with options that each have detailed descriptions in Bootstrap popovers. Despite trying different values for the placement option in the popover initialization, the popover consistently appears in the mi ...

Implementing AJAX to dynamically update product categories on the homepage

I am embarking on a project that I find quite challenging and would greatly appreciate any help in the form of ideas, tutorials, or examples. Our client has requested a step-by-step system to be implemented on the homepage, allowing customers to choose be ...

Tips for concealing the Google Chrome status bar from appearing on a webpage

I have been intrigued by the rise of Progressive Web Apps (PWAs) and I am eager to dive into understanding them better. One common feature I have noticed in PWAs is the ability to hide the browser chrome, including the URL bar, back button, search fields, ...

Changing the Size of X-Axis Labels Font in a Line Chart Using ChartJS

Struggling with adjusting the font size of x-axis labels in my line chart design using ChartJS. I've attempted to set the x-axis ticks to my desired size, but the font size remains unchanged: options:{ scales:{ xAxis: [{ ...

Javascript is throwing an unexpected token error due to an HTML alteration, which is indicated by the character

Each time I make a modification to my HTML file, I encounter a javascript error saying Unexpected token '!'. It appears that any change made to the HTML file triggers a javascript error. Despite this, there is nothing incorrect with the code itse ...

Unable to fix position: sticky issue following Angular 15 update

Following the angular material update to version 15, many of us experienced issues with CSS changes breaking. This also affected legacy code that included sticky headers. <div class="row"> <div class="col-12"> <di ...

Changing the background color and text color on hover using CSS

Is there a way for the menu to change both the background and text color when I hover over it? Currently, my CSS only changes the background color but not the text color. Any help would be appreciated. a:hover { color: #231f20; //for new text co ...

Animating jQuery to adjust height based on a percentage

My animation using percentage is not working as expected. It expands to the whole height instantly instead of smoothly transitioning to 100%. Here is my CSS code: #block-views{ overflow:hidden; height:20px; } This is my HTML code: <div id="block-view ...

How can I color the first, second, and third buttons when the third button is clicked? And how can I color all the buttons when the fourth button is clicked?

I am trying to achieve a task with 4 buttons. When the third button is clicked, I want the first, second, and third buttons to change color. Similarly, when the fourth button is clicked, I want all buttons to change color. Additionally, I need to save a va ...

How come Bootstrap isn't functioning despite being properly loaded in my Django project?

I recently started working on a Django project and encountered some confusion while integrating Bootstrap3 for the front-end. After successfully installing django-bootstrap3 using pip in my command prompt, I added it as a third-party app in my settings.py ...

Is the ng bootstrap modal within Angular failing to show up on the screen

In the midst of working on my project, I encountered an issue with opening a modal using ng bootstrap. Although I found a similar thread discussing this problem, it did not include bootstrap css. I decided to reference this example in hopes of resolving t ...