Sagar Rawal
Sagar Rawal

Reputation: 399

Use Two styles in an elements.style properties

Hellow everyone,

I'm using the below code to Highlight the row of an table. I was wondering whether it is possible to add marquee effects ( i.e moving text from left to right ) i.e

<marquee>TEXT</marquee>

in same row. For example say now a current time is 12:45 Am then, it'd select class ra1 and have red bckground. At same time , I want to add marquee effects in same active row and when after 45 minutes, another row i.e class raa3 will be active and will have red background. I want to have now marquee effects in this row now i.e class ra3 which is currently selected and active.

function openCity(evt, cityName, today) {
  var i, tabcontent, tablinks;
  tabcontent =
    document.getElementsByClassName("tabcontent");
  for (i = 0; i < tabcontent.length; i++) {
    tabcontent[i].style.display = "none";
  }
  tablinks = document.getElementsByClassName("tablinks");
  for (i = 0; i < tablinks.length; i++) {
    tablinks[i].className = tablinks[i].className.replace(" active", "");
  }
  document.getElementById(cityName).style.display = "block";
  if (typeof today == 'undefined') {
    evt.currentTarget.className += " active";
  } else {
    tablinks[today].className += " active";
  }
}

let now = new Date().getHours() * 100 + new
Date().getMinutes();
let times = [45, 100, 145];
let classes = ['ra1', 'ra3', ];
let selected = classes[times.reduce((acc, curr, idx) => now >=
  curr ? idx : acc)];
if (selected){
  var elements = document.getElementsByClassName(selected);
  for(var i=0;i<elements.length; i++)
      elements[i].style.background = 'red';
}
.tabcontent { display: none }
<div class="tab" style="margin-top: 1.6%;  ">
  <button class="tablinks" onclick="openCity(event, 'Sun0') ">Sun</button>
  <button class="tablinks" onclick="openCity(event, 'Mon0')">Mon</button>

</div>

<div id="Sun0" class="tabcontent" id="np">


  <div style="overflow-x:auto;">
    <table>
      <col width="65%">
      <col width="35%">
      <tr>
        <th>Class</th>
        <th>Time</th>
      </tr>

      <tr>
        <td class="ra1">10:15-11:00AM</td>
        <td style="background:black; color:Yellow;">It is Sunday </td>

      </tr>
      <tr>
        <td class="ra3">11:00-11:45AM</td>
        <td style="background:black; color:Yellow;">It is Sunday</td>

      </tr>
    </table>
  </div>
</div>


<div id="Mon0" class="tabcontent">

  <!--Monnday-->
  <div style="overflow-x:auto;">
    <table>
      <col width="65%">
      <col width="35%">
      <tr>
        <th>Class</th>
        <th>Time</th>
      </tr>

      <tr>
        <td class="ra1">10:15-11:00AM</td>
        <td style="background:black; color:Yellow;">It is Monday</td>

      </tr>
      <tr>
        <td class="ra3">11:00-11:45AM</td>
        <td style="background:black; color:Yellow;">It is Monday</td>

      </tr>
    </table>
  </div>
</div>

Upvotes: 0

Views: 48

Answers (1)

Dij
Dij

Reputation: 9808

you can get everything inside the selected div using innerHTML and then you can wrap it inside "" tags using elements[i].innerHTML = "<marquee>" + elements[i].innerHTML + "</marquee>"

function openCity(evt, cityName, today) {
  var i, tabcontent, tablinks;
  tabcontent =
    document.getElementsByClassName("tabcontent");
  for (i = 0; i < tabcontent.length; i++) {
    tabcontent[i].style.display = "none";
  }
  tablinks = document.getElementsByClassName("tablinks");
  for (i = 0; i < tablinks.length; i++) {
    tablinks[i].className = tablinks[i].className.replace(" active", "");
  }
  document.getElementById(cityName).style.display = "block";
  if (typeof today == 'undefined') {
    evt.currentTarget.className += " active";
  } else {
    tablinks[today].className += " active";
  }
}

let now = new Date().getHours() * 100 + new
Date().getMinutes();
let times = [45, 100, 145];
let classes = ['ra1', 'ra3', ];
let selected = classes[times.reduce((acc, curr, idx) => now >=
  curr ? idx : acc)];
if (selected){
  var elements = document.getElementsByClassName(selected);
  for(var i=0;i<elements.length; i++){
      elements[i].style.background = 'red';
      elements[i].innerHTML = "<marquee>" + elements[i].innerHTML + "</marquee>";
 }
}
.tabcontent { display: none }
<div class="tab" style="margin-top: 1.6%;  ">
  <button class="tablinks" onclick="openCity(event, 'Sun0') ">Sun</button>
  <button class="tablinks" onclick="openCity(event, 'Mon0')">Mon</button>

</div>

<div id="Sun0" class="tabcontent" id="np">


  <div style="overflow-x:auto;">
    <table>
      <col width="65%">
      <col width="35%">
      <tr>
        <th>Class</th>
        <th>Time</th>
      </tr>

      <tr>
        <td class="ra1">10:15-11:00AM</td>
        <td style="background:black; color:Yellow;">It is Sunday </td>

      </tr>
      <tr>
        <td class="ra3">11:00-11:45AM</td>
        <td style="background:black; color:Yellow;">It is Sunday</td>

      </tr>
    </table>
  </div>
</div>


<div id="Mon0" class="tabcontent">

  <!--Monnday-->
  <div style="overflow-x:auto;">
    <table>
      <col width="65%">
      <col width="35%">
      <tr>
        <th>Class</th>
        <th>Time</th>
      </tr>

      <tr>
        <td class="ra1">10:15-11:00AM</td>
        <td style="background:black; color:Yellow;">It is Monday</td>

      </tr>
      <tr>
        <td class="ra3">11:00-11:45AM</td>
        <td style="background:black; color:Yellow;">It is Monday</td>

      </tr>
    </table>
  </div>
</div>

Upvotes: 1

Related Questions