Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get and set value to html tag using array in javascript?

I want to highlight color to many rows if its date value passes my condition as below code

I'm retrieving from_date and to_date from database by using foreach() in php and I will extract it into rows of table and I want to highlight with red color to some rows of table if from_date and to_date value pass my condition as below javascript

But I don't know how to do it with javascript code to find all id selector of html and set its color to red,black,blue if from_date and to_date passes condition.

<table>
   <tr class="highlight">
      <td>ID:</td>
      <td id="from">From Date: 2014-11-28</td>
      <td id="to">To Date: 2014-11-30</td>
   </tr>
   <tr class="highlight">
      <td>ID:</td>
      <td id="from">From Date: 2014-11-28</td>
      <td id="to">To Date: 2014-11-28</td>
   </tr>
   <tr class="highlight">
     <td>ID:</td>
     <td id="from">From Date: 2014-11-28</td>
     <td id="to">To Date: 2014-11-28</td>
  </tr>
  <tr class="highlight">
     <td>ID:</td>
     <td id="from">From Date: 2014-11-28</td>
     <td id="to">To Date: 2014-12-01</td>
  </tr>
  <tr class="highlight">
     <td>ID:</td>
     <td id="from">From Date: 2015-01-01</td>
     <td id="to">To Date: 2015-01-30</td>
  </tr>
</tabl>

As above html code I want to use array in javascript to check all id html selector to check its from_date and to_date value and set red color to highlight class (class="highlight") by condition.

            <script> 
            var today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
            var from_day = document.getElementById("from");
            var to_day   = document.getElementById("to");
            var highlight = [];

            var get_from_day = new Date(from_day);
            var get_to_day   = new Date(to_day);

            var i = document.getElementById();

            if(get_from_day.getTime() == get_to_day.getTime()){
                 highlight[] = "red";
                        }
            if(get_from_day.getTime() > get_to_day.getTime()){
                highlight[] = "blue";
                        }
            if(get_to_day.getTime() == today.getTime()){
                highlight[] = "black";
            }else{
                if(highlight){
                    for(i=0;i<=;i++){
            document.getElementById("highlight").style.background-color = highlight;
                        }
                }
            } 
        </script>

Thanks advance for help

like image 250
Cootel Kh Avatar asked Sep 20 '26 03:09

Cootel Kh


1 Answers

assign fromDate and toDate as class name to eg.

<td class="fromDate " id="<?php echo "from".$i;?>">from date</td>
<td class="toDate"  id="<?php echo "to".$i;?>">To date</td>

try this

$('#mytab1 tr').each(function(){

   $(this).find('td').each(function(){
      if($(this).attr("class")=="fromDate")
      {
        var fromdata=$(this).text();
        // Apply your checking and applying color logic here
      }
      else if($(this).attr("class")=="toDate")
      { 
         var todata=$(this).text();
        // Apply your checking and applying color logic here
      }
   });
});
like image 194
Atul Nar Avatar answered Sep 22 '26 16:09

Atul Nar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!