Javascript change label forcolor based on condition

Hello, given my code below. How can I find the lblmyDate inside of the rptItems repeater and change the forecolor of the label to red If the date is 30 days older than today???


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <script type="text/javascript">

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Repeater ID="rptItems" runat="server">
            <ItemTemplate>
                <table>
                    <tr>
                        <td>
                             <asp:Label id="lblMyDate" runat="server" Text='<%#Eval("myDate")%>'></asp:Label>
                        </td>
                    </tr>
                </table>
           </ItemTemplate>
            <AlternatingItemTemplate>
                <table>
                    <tr>
                        <td>
                             <asp:Label id="lblMyDate" runat="server" Text='<%#Eval("myDate")%>'></asp:Label>
                        </td>
                    </tr>
                </table> 
            </AlternatingItemTemplate>
        </asp:Repeater>
       </div>
    </form>
</body>
</html>

Open in new window

Scott BaldridgeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Randy PooleCommented:
You would do this on the code behind on each item row
0
Scott BaldridgeAuthor Commented:
I have done it on the itemdatabaound of the repeater. I'm trying to see how to do it in javascript.

Performance wise is it best to do this type of work in javascript or code behind?
0
Randy PooleCommented:
Since you are not editing the data on the repeater (I assume) , then there would be no benefit to using javascript to perform this action.  Now if you allowed them to edit rows of data and wanted to resubmit it all at once and do something of this sort, then javascript would be a good choice.  I do have to tell you, it is a pain in the butt though since you have to build an array of client ID's which match the various data on the page.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott BaldridgeAuthor Commented:
I agree and will stick to the code behind. Thanks for your time.
0
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
It is best to do this type of thing in js/jquery.  This is a great job for momentjs  http://momentjs.com/

http://jsbin.com/dedet/1/edit
<!DOCTYPE html>
<html>
<head>
  <style>
    .red{color:red;}
    .black{color:black;}
    </style>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
  <script src="http://jsbin.com/wabico/1.js"></script>
<script>
  $(function () {
    $('.checkdate').each(function () {
        var dateText = $(this).text();
        var convertDate = moment(dateText, "M/D/YYYY");
        var today = moment();
        var days = convertDate.diff(today, 'days');
        if (days < -30) {
            $(this).removeClass('black').addClass('red');
        } else {
            $(this).removeClass('red').addClass('black');
        }

    });

});
  </script>
  <meta charset="utf-8">
  <title>padas</title>
</head>
<body>
 <table>
                    <tr>
                        <td class="checkdate">
                          4/1/2014
                        </td>
                    </tr>
                </table> 
   <table>
                    <tr>
                        <td class="checkdate">
                          7/1/2014
                        </td>
                    </tr>
                </table> 
</body>
</html>

Open in new window

Just a reminder that you can't repeat ID's.  They need to be unique.  You can repeat classes.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.