1

If I want to add an CSS style for a field in a table that has the value that is equal to the current time or greater than the current time but it is less than the value of the next field, how can I do this using Jquery?

The sample of table looks like this:

 -----------------------
  field |  current time
 -----------------------
    1   |    05:25
    2   |    07:30
    3   |    09:18
    4   |    10:13
    5   |    12:44

If the current time is 09:50 the field No.4 should have a different background color (If the current time is 05:30 then the field No. 2 , ...)

Thanks for all suggestions.

2
  • have you tried anything so far? Commented Sep 26, 2011 at 12:55
  • Why wouldn't you do that on server side? Commented Sep 26, 2011 at 12:56

2 Answers 2

1

assuming this is ordered...

1)you should have a function ( myFunc) that can compare times and return if left argument bigger or smaller then the right.

2)you should have a function that returns the 'now' time.

3)each time span should be wrap with class .time

then in jQuery : each time should be wrap with class .time

   $(".time").each(function (){
    if (myfunc ($(this).text(),now())=='bigger')

{
 $(this).css('color','red');
return false;
}


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

Comments

0

Cobbled this together: http://jsfiddle.net/xVDnj/1/ -- It will assign a class "past" or "future" to each table cell depending on whether the given time is before or after the current time (on the same day).

$(document).ready(function() {
    var cdate = new Date();
    $('table tbody tr').each(function() {
        var $tc = $(this).find('td').eq(1)
        var tstr = $tc.text();
        var thour = tstr.split(":")[0];
        var tmin = tstr.split(":")[1];
        var tdate = new Date();
        tdate.setHours(thour,tmin);
        if (tdate-cdate < 0) { 
            $tc.addClass('past');
        } else {
            $tc.addClass('future');
        }
    });
});

Comments

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.