Adjust the hyperlink color of <a> to change when hovering over <tr>

When I hover over a <tr> tag, I want the color of the associated <a> element to change to white.

I attempted to achieve this using jQuery in the following way:

<script>
    $('tr').hover(function(){
        $('a').css('color','white');
    });
</script>

However, this code ends up changing the text color for all <tr> elements. Any suggestions on how to fix this?

Answer №1

Simply include this line in your CSS stylesheet and eliminate the need for jQuery:

tr:hover a { color: white; }

If you are determined to make your jQuery script work, then make sure to target only the links within the hovered row:

$('tr').hover(function(){
   $('a', this).css('color','white');
 });

Answer №2

If you want to change the color of an element when hovering over it, there are a few ways to do so. One way is to use the .hover method in JavaScript (jquery) where you can specify two functions - one for when the mouse moves over the element and another for when it moves out. Here's an example:

1 - Using JavaScript (jquery)

$(document).ready(function(){
  $('tr').hover(
    function(){
      var $this = $(this);
      $this.css('background-color', '#fff');
    },
    function(){
      var $this = $(this);
      // original color -> $this.css('background-color', $this.data('bgcolor'));
      $this.css('background-color', '#000');
    }
  );    
});

You can retrieve the original color using .data('bgcolor'), which gets the color set by the style attribute or in the original CSS.

2) Alternatively, you can simply add a CSS rule like this:

.row {
   background-color: #000;
}

.row:hover {
   background-color: #fff;
}

and apply it to the row element like this:

<tr class='row'>
...
</tr>

Answer №3

In my situation, I am working on a website with a primary color and white as the secondary color. To add some variety, I decided to randomize the color each time the page is refreshed. This led me to create hover elements that would change the color to the primary one. Here is the code snippet I ended up using:

#menu-buttons>li{ color:#fff; }
#menu-buttons>li:hover { color: inherit; }

After setting up the hover effect, all I had to do was assign the randomized color to the parent element like this:

$('#menu-buttons').css('color',mainColor);

Answer №4

Dealing with the same problem, I stumbled upon this solution which turned out to work wonders.

     div.cntrblk tr:hover td a {
       color:rgb(0,0,255);
     }

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

When using Math.floor(Math.random() * val.length), the result will be a letter rather than a number

Looking to implement a feature where a different background image is displayed randomly upon page visit or refresh. Currently, specifying the images in an array like the example below works well, but I want to be able to pull from a folder. $(document).r ...

Link embedded in prism formatting, encased in code snippet

In the HTML template, I have the following code snippet: <pre> <code class="language-markup"> {% filter force_escape %} <Item> <MarkUp><a href="http://google.com">Google</a></MarkUp> <No ...

How can I align a div in a vertical manner if it is displayed as a circle?

This is the creation I have come up with: This is the design I am aiming for: Below is the code I have written: <div id="OR"><span style=" vertical-align: middle;background:blue;">OR</span></div> Here is the corresponding CSS: ...

Enhancing a popup with animated effects

I have been working on a popup that I want to add a subtle animation to. A fade effect seems like the perfect solution. Here is the code for the button: <a href="javascript:void(0)" onclick="document.getElementById('back_overlay').style.disp ...

Is it possible to conceal an error message while another error message is being shown?

<form name="form"> <input type="password" placeholder="Password" ng-model="password" name="password" minlength="6" maxlength="15" ng-pattern="/^\S*$/" required> <span ng-show="form.password.$dirty && form.password.$in ...

Track the number of views each month using PHP and MySQL to generate dynamic jQuery charts

I am currently utilizing jQuery with chart.js to display the view counter based on month using PHP and MySql. Each page view is inserted into MySql in the following format : | id | ip | page | date(timestamp) | | 1 | 138.86.20.20 | test.p ...

Font Awesome icons occasionally display as squares, but typically they render correctly

I've noticed a strange issue on my website where the font awesome icons sometimes display as squares instead of their intended design. After searching through forums, it seems that my situation is different from others experiencing similar problems. I ...

Troublesome behavior from Bootstrap's datepicker

Recently, I decided to dive into front-end development using Bootstrap but I'm facing some challenges. There are two issues here. The calendar glyph is appearing under the date input field instead of at the end of it on the same line. The picker is ...

Keep the footer at the bottom of the screen without needing to define a 100vh in Material UI

In the process of developing my react app with MUI framework, I encountered various challenges in creating a sticky footer at the bottom of my screen. After exploring different solutions, one approach that I found most satisfactory is as follows: export de ...

Struggling to devise a for loop for a JavaScript-generated list

I'm attempting to loop the content of an H1 element through a list 10 times. I seem to have made a mistake in my code and would appreciate any guidance. var headOne = document.createElement("H1"); headOne.textContent = "Hello World"; document.body. ...

Tips for positioning HTML elements at the exact mouse location as it moves, without any delay?

Currently, I am in the process of developing a web-based drawing application that does not rely on using a canvas. My decision to avoid using a canvas is because I plan to incorporate CSS Keyframes into the HTML elements upon placement. This approach allow ...

The primary view seamlessly integrates with the page following the invocation of the partial view

Whenever the button is clicked, a different partial view is returned based on the selected value from the drop-down list. Controller: [HttpPost] public ActionResult Foo(SomeViewModel VM) { var model = VM; if (Request.IsAjaxRequest()) { ...

Manipulate SVG elements by dragging them along the boundary of the path

Looking to achieve a specific functionality where I can drag and drop an element along the edges of a drawn path, rather than inside the path itself. To clarify, the goal is to move the red marked element on the black line bordering the path, allowing move ...

The fancybox scroll feature is visible when using a browser, but unfortunately, it does not appear on the

I noticed that the scrollbar in my fancybox is not showing up on iPad, but it does display when using Google Chrome on Desktop. You can find the page here. The issue seems to be with the vertical scroll bar not appearing on iPad. Has anyone encountered th ...

Angular - creating adaptive HTML container with CSS styling

I am trying to achieve a layout similar to the one shown in the image. A container can have multiple elements starting within it or no elements at all. The elements always have a fixed height, and the container's height is determined by the number of ...

Enhance jQuery with additional features using Bootstrap

My website utilizes Bootstrap 4, which comes with the slim version of jQuery that does not include effects. I attempted to add the normal version of jQuery alongside the slim version in order to access the effects, but it was unsuccessful. I even tried r ...

Converting a jQuery/JSON/PHP object to a C# function

Hello everyone, I am new to JSON and I recently came across an example that uses PHP and jQuery (http://wil-linssen.com/entry/extending-the-jquery-sortable-with-ajax-mysql/). My goal is to take the serialized "order" object and utilize it in a C# method j ...

Ember's distinctive feature: Named Block Helpers

Can we create "named blocks" in a different way? For instance, {{#customBlock "repeatableBlock"}} {{!-- block containing numerous properties and data that may become messy if hardcoded multiple times --}} {{/customBlock}} Then, elsewhere in the code, {{ ...

Can the value of each element in an array be modified if the items are not arranged in consecutive order (e.g. 0, 1, 2, etc.)?

I am facing a challenge with an array where items are stored using other elements' ID values instead of integers. I need to change the boolean values of each item in this array to 'false' when a button is clicked. Since my knowledge is limit ...

Adjust ChartJS yAxes "tick marks"

I'm having trouble adjusting the scales on my yAxes and all the information I find seems to be outdated. My goal is to set my yAxes range from 0 to 100 with steps of 25. Check out this link yAxes: [ { ...