0

I'm trying to keep current CSS value of an element in a variable. My problem is whenever I change style of original element, the value in variable which I've assigned before changes too.

var currentColor;

$("tr").hover(function () {
    currentColor = $(this).css("background-color");
    $(this).css("background-color", "#f7f7f7")
}).mouseout(function () {
    $(this).css("background-color", currentColor);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
    <tr style="background-color:#FF0000"><td>Foo</td></tr>
    <tr style="background-color:#00FF00"><td>Foo</td></tr>
    <tr style="background-color:#0000FF"><td>Foo</td></tr>
</table>

I also tried to use .data() but it didn't solve my problem.

var currentColor;

$("tr").hover(function () {
    $("div").data("currentColor", $(this).css("background-color"));
    $(this).css("background-color", "#f7f7f7")
}).mouseout(function () {
    $(this).css("background-color", $("div").data("currentColor"));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
    <tr style="background-color:#FF0000"><td>Foo</td></tr>
    <tr style="background-color:#00FF00"><td>Foo</td></tr>
    <tr style="background-color:#0000FF"><td>Foo</td></tr>
</table>

UPDATE

Since community ask me why I haven't use tr:hover {background:color} I would have to include that in my actual project, and in that particular case I were into a situation that I had to modify back-ground color using jQuery. After that tr:hover {background:color} don't work and you have to manage tr:hover functionality with jQuery too.

I excluded that part from my question because I thought its not necessary to explain it.

8
  • 2
    out of my curiosity, why are you using jQuery to change the background colour on hover? in CSS it's just tr:hover { background : #f7f7f7; } Commented May 15, 2018 at 8:05
  • @fcalderan because once you use $("tr").css("background-color", "some color"), after that tr:hover { background : #f7f7f7; } don't work. Commented May 15, 2018 at 8:13
  • 2
    I would recommend you to learn HTML & CSS if you have just started working in the front-end. Commented May 15, 2018 at 8:13
  • All I see from your code is that your rows have a background-colour that change when they are hovered. If there's nothing more and no specific reason to use js, then CSS is the way. Commented May 15, 2018 at 8:21
  • 1
    I am sorry if my comment offended you. Didn't mean to insult you. Since you have hard-coded the color #f7f7f7 in your mouseover event, I strongly believe it could be handled in CSS itself. In your case there is no need of JQuery or Javascript itself. I have also seen many recommending the same solution. Commented May 15, 2018 at 8:32

2 Answers 2

5

Use this code:

$("tr").mouseenter(function() {
  $(this).attr("currentColor", $(this).css("background-color"));
  $(this).css("background-color", "#f7f7f7")
}).mouseout(function() {
  $(this).css("background-color", $(this).attr("currentColor"));
});

I don't know why you are using $("div") since you have no div in your HTML.

Second, use mouseenter and not hover

jQuery Demo

$("tr").mouseenter(function() {
  $(this).attr("currentColor", $(this).css("background-color")).css("background-color", "#f7f7f7")
}).mouseout(function() {
  $(this).css("background-color", $(this).attr("currentColor"));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr style="background-color:#FF0000">
    <td>Foo</td>
  </tr>
  <tr style="background-color:#00FF00">
    <td>Foo</td>
  </tr>
  <tr style="background-color:#0000FF">
    <td>Foo</td>
  </tr>
</table>

Css Way demo

table tbody tr:hover td {
  background-color: #f7f7f7;
}
<table>
  <tr style="background-color:#FF0000">
    <td>Foo</td>
  </tr>
  <tr style="background-color:#00FF00">
    <td>Foo</td>
  </tr>
  <tr style="background-color:#0000FF">
    <td>Foo</td>
  </tr>
</table>

Sign up to request clarification or add additional context in comments.

4 Comments

Much appreciated
@MasoudKeshavarz No problem happy to help
Why CSS demo? My question is clear keep current CSS value in variable for later use. I never asked How can I do this with CSS? :(
I added it to be nice, I never said you had to use it. Other might want to use it.
1

you can do it with PURE CSS

td:hover{
background-color:#f7f7f7;
}
<table>
  <tr style="background-color:#FF0000">
    <td>Foo</td>
  </tr>
  <tr style="background-color:#00FF00">
    <td>Foo</td>
  </tr>
  <tr style="background-color:#0000FF">
    <td>Foo</td>
  </tr>
</table>

Here you can find explain :hover in css:https://www.w3schools.com/cssref/css_selectors.asp

3 Comments

I know, but once you change background with jQuery for example using $("tr").css("background-color", "some color"), after that tr:hover { background : #f7f7f7; } don't work.
please specify what do you mean with "after that". Are you mixing css and javascript together?
@לבנימלכה please read my update. I am sorry because I didn't explain it earlier. I thought it is not necessary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.