RiotAct
RiotAct

Reputation: 773

Use jQuery to add class to next table cell in the column

I am creating some special hover effects for FullCalendar version 3. The way it is generated now has a single day split into separate tables making it a bit of a challenge to add hover effects with CSS. I have most of this working but one last little bit and I'm not sure how to get what I need.

Here is a bit of sample code for a single week:

This is the first table, the background of the cell.

<div class="fc-bg">
<table class="">
  <tbody>
    <tr>
      <td class="fc-day fc-widget-content fc-sun fc-past" data-date="2018-04-01"></td>
      <td class="fc-day fc-widget-content fc-mon fc-past" data-date="2018-04-02"></td>
      <td class="fc-day fc-widget-content fc-tue fc-past" data-date="2018-04-03"></td>
      <td class="fc-day fc-widget-content fc-wed fc-past" data-date="2018-04-04"></td>
      <td class="fc-day fc-widget-content fc-thu fc-past" data-date="2018-04-05"></td>
      <td class="fc-day fc-widget-content fc-fri fc-past" data-date="2018-04-06"></td>
      <td class="fc-day fc-widget-content fc-sat fc-past" data-date="2018-04-07"></td>
    </tr>
  </tbody>
</table>

This is the 2nd table, with the top of the cell in the thead and the events listed in the tbody:

<div class="fc-content-skeleton">
    <table>
      <thead>
        <tr>
          <td class="fc-day-top fc-sun fc-past" data-date="2018-04-01">
            <a class="fc-day-number" data-goto="{&quot;date&quot;:&quot;2018-04-01&quot;,&quot;type&quot;:&quot;day&quot;}">1</a>
          </td>
          <td class="fc-day-top fc-mon fc-past" data-date="2018-04-02">
            <a class="fc-day-number" data-goto="{&quot;date&quot;:&quot;2018-04-02&quot;,&quot;type&quot;:&quot;day&quot;}">2</a>
          </td>
          <td class="fc-day-top fc-tue fc-past" data-date="2018-04-03">
            <a class="fc-day-number" data-goto="{&quot;date&quot;:&quot;2018-04-03&quot;,&quot;type&quot;:&quot;day&quot;}">3</a>
          </td>
          <td class="fc-day-top fc-wed fc-past" data-date="2018-04-04">
            <a class="fc-day-number" data-goto="{&quot;date&quot;:&quot;2018-04-04&quot;,&quot;type&quot;:&quot;day&quot;}">4</a>
          </td>
          <td class="fc-day-top fc-thu fc-past" data-date="2018-04-05">
            <a class="fc-day-number" data-goto="{&quot;date&quot;:&quot;2018-04-05&quot;,&quot;type&quot;:&quot;day&quot;}">5</a>
          </td>
          <td class="fc-day-top fc-fri fc-past" data-date="2018-04-06">
            <a class="fc-day-number" data-goto="{&quot;date&quot;:&quot;2018-04-06&quot;,&quot;type&quot;:&quot;day&quot;}">6</a>
          </td>
          <td class="fc-day-top fc-sat fc-past" data-date="2018-04-07">
            <a class="fc-day-number" data-goto="{&quot;date&quot;:&quot;2018-04-07&quot;,&quot;type&quot;:&quot;day&quot;}">7</a>
          </td>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td class="fc-event-container">
            <a class="fc-day-grid-event fc-h-event fc-event fc-start fc-end">
            <div class="fc-content">
              <span class="fc-title">All Day Event</span>
            </div></a>
          </td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td class="fc-event-container">
            <a class="fc-day-grid-event fc-h-event fc-event fc-start fc-not-end">
            <div class="fc-content">
              <span class="fc-title">Long Event</span>
            </div></a>
          </td>
        </tr>
      </tbody>
    </table>
  </div>

Ok, so I have created a bit of jQuery to add a hover effect when the fc-day or fc-day-top classes are hovered. It looks like this:

$('.fc-day, .fc-day-top').hover(function() {
            var myEm = $(this).attr('data-date');
                $('.fc-day-top[data-date = '+myEm+'] .fc-day-number').addClass('on-hover');
          }, function() {
                var myEm = $(this).attr('data-date');
            $('.fc-day-top[data-date = '+myEm+'] .fc-day-number').removeClass('on-hover');
          });

Now the last part I need, is to be able to index the cell that is being hovered, and apply a class to the same column td in the tbody section of the 2nd table. Basically, I am applying the hover effect 3 times because of the way each day is output.

So when I hover <td> in the first column of the 2nd table body, how can I index that so I can apply the necessary css for the corresponding columns?

Upvotes: 0

Views: 424

Answers (1)

Mark Schultheiss
Mark Schultheiss

Reputation: 34158

Example to highlight headers of each table based on index.

$('.fc-day, .fc-day-top').hover(function() {
  let myEm = $(this).attr('data-date');
  let myIndex = $(this).index();
  let targets = $('.fc-day');
  let targets1 = $('.fc-day-top');
  targets.add(targets1).removeClass('show-me');

  // console.log(myIndex);
  $('.fc-day').text(myIndex);
  targets.eq(myIndex).addClass('show-me');
  targets1.eq(myIndex).addClass('show-me');
  $('.fc-day-top[data-date = ' + myEm + '] .fc-day-number').addClass('on-hover');
}, function() {
  let myEm = $(this).attr('data-date');
  let myIndex = $(this).index();
  let targets = $('.fc-day');
  let targets1 = $('.fc-day-top');
  targets.add(targets1).removeClass('show-me');
  targets.eq(myIndex).addClass('show-me');
  targets1.eq(myIndex).addClass('show-me');
  $('.fc-day').text(myIndex);
  $('.fc-day-top[data-date = ' + myEm + '] .fc-day-number').removeClass('on-hover');
});
table.secondtable>tbody>tr>td,
.fc-day-top,
.fc-day {
  border: solid lime 1px;
  width: 2em;
  height: 2em;
}

.show-me {
  border: solid green 1px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table class="">
  <tbody>
    <tr>
      <td class="fc-day fc-widget-content fc-sun fc-past" data-date="2018-04-01"></td>
      <td class="fc-day fc-widget-content fc-mon fc-past" data-date="2018-04-02"></td>
      <td class="fc-day fc-widget-content fc-tue fc-past" data-date="2018-04-03"></td>
      <td class="fc-day fc-widget-content fc-wed fc-past" data-date="2018-04-04"></td>
      <td class="fc-day fc-widget-content fc-thu fc-past" data-date="2018-04-05"></td>
      <td class="fc-day fc-widget-content fc-fri fc-past" data-date="2018-04-06"></td>
      <td class="fc-day fc-widget-content fc-sat fc-past" data-date="2018-04-07"></td>
    </tr>
  </tbody>
</table>


<div class="fc-content-skeleton">
  <table class="secondtable">
    <thead>
      <tr>
        <td class="fc-day-top fc-sun fc-past" data-date="2018-04-01">
          <a class="fc-day-number" data-goto="{&quot;date&quot;:&quot;2018-04-01&quot;,&quot;type&quot;:&quot;day&quot;}">1</a>
        </td>
        <td class="fc-day-top fc-mon fc-past" data-date="2018-04-02">
          <a class="fc-day-number" data-goto="{&quot;date&quot;:&quot;2018-04-02&quot;,&quot;type&quot;:&quot;day&quot;}">2</a>
        </td>
        <td class="fc-day-top fc-tue fc-past" data-date="2018-04-03">
          <a class="fc-day-number" data-goto="{&quot;date&quot;:&quot;2018-04-03&quot;,&quot;type&quot;:&quot;day&quot;}">3</a>
        </td>
        <td class="fc-day-top fc-wed fc-past" data-date="2018-04-04">
          <a class="fc-day-number" data-goto="{&quot;date&quot;:&quot;2018-04-04&quot;,&quot;type&quot;:&quot;day&quot;}">4</a>
        </td>
        <td class="fc-day-top fc-thu fc-past" data-date="2018-04-05">
          <a class="fc-day-number" data-goto="{&quot;date&quot;:&quot;2018-04-05&quot;,&quot;type&quot;:&quot;day&quot;}">5</a>
        </td>
        <td class="fc-day-top fc-fri fc-past" data-date="2018-04-06">
          <a class="fc-day-number" data-goto="{&quot;date&quot;:&quot;2018-04-06&quot;,&quot;type&quot;:&quot;day&quot;}">6</a>
        </td>
        <td class="fc-day-top fc-sat fc-past" data-date="2018-04-07">
          <a class="fc-day-number" data-goto="{&quot;date&quot;:&quot;2018-04-07&quot;,&quot;type&quot;:&quot;day&quot;}">7</a>
        </td>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td class="fc-event-container">
          <a class="fc-day-grid-event fc-h-event fc-event fc-start fc-end">
            <div class="fc-content">
              <span class="fc-title">All Day Event</span>
            </div>
          </a>
        </td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td class="fc-event-container">
          <a class="fc-day-grid-event fc-h-event fc-event fc-start fc-not-end">
            <div class="fc-content">
              <span class="fc-title">Long Event</span>
            </div>
          </a>
        </td>
      </tr>
    </tbody>
  </table>
</div>

Upvotes: 1

Related Questions