Search code examples
jquerycss-selectorscycle

Change class background on hover


I have table with multiple elements with class like 'rok0', 'rok1', 'rok2' etc. and I want to change background of all elements with same class when hover on any of them. I got this function:

$(function() {
  $('.rok1').hover(function() {
    $('.rok1').css('background-color', 'yellow');
  }, function() {
    $('.rok1').css('background-color', '');
  });
});

This function is working, but i would like to use it for all the classes, so I want to use for cycle on it but somehow it doesn't work.

I tried this:

$(function() {
  for (i = 0; i < 20; i++) { 
    console.log('.rok'+i);
    $('.rok'+i).hover(function() {
      $('.rok'+i).css('background-color', 'yellow');
    }, function() {
      $('.rok'+i).css('background-color', '');
    });
  }
});

and this:

for (i = 0; i < 20; i++) { 
  $(function() {
    console.log('.rok'+i);
    $('.rok'+i).hover(function() {
      $('.rok'+i).css('background-color', 'yellow');
    }, function() {
      $('.rok'+i).css('background-color', '');
    });
  }); 
}

None of them was working, I have no idea why, can you help me?

Edit: Example of my table:

<table>
<tr>
<th class='rok0'>Col11</th>
<th class='rok1'>Col21</th>
<th class='rok2'>Col31</th>
</tr>
<tr>
<th class='rok0'>Col12</th>
<th class='rok1'>Col22</th>
<th class='rok2'>Col32</th>
</tr>
<tr>
<td class='rok0'>Col13</td>
<td class='rok1'>Col23</td>
<td class='rok2'>Col33</td>
</tr>
</table>

And I want to set background of all element with the SAME class when I hover over one of them.


Solution

  • You can make use of startsWith css attribute in jquery and add classes accordingly without any looping.

    $(function() {
      $('[class^="rok"]').hover(function() {
        $('[class^="rok"]').css('background-color', 'yellow');
      }, function() {
        // on mouseout, reset the background colour
        $('[class^="rok"]').css('background-color', '');
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="rok1">
      Rok1
    </div>
    
    <div class="rok2">
      Rok2
    </div>
    
    <div class="rok3">
      Rok3
    </div>

    Update

    Here's how you can do for same class with startswith css selector.

    var currClass;
    
    $(function() {
      $('[class^="rok"]').hover(function() {
        currClass = $(this).attr('class');
        $('.' + currClass).css('background-color', 'yellow');
      }, function() {
        currClass = $(this).attr('class');
        // on mouseout, reset the background colour
        $('.' + currClass).css('background-color', '');
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <table>
      <tr>
        <th class='rok0'>Col11</th>
        <th class='rok1'>Col21</th>
        <th class='rok2'>Col31</th>
      </tr>
      <tr>
        <th class='rok0'>Col12</th>
        <th class='rok1'>Col22</th>
        <th class='rok2'>Col32</th>
      </tr>
      <tr>
        <td class='rok0'>Col13</td>
        <td class='rok1'>Col23</td>
        <td class='rok2'>Col33</td>
      </tr>
    </table>