tags:

views:

32

answers:

2

Good morning,

I am having trouble with the following code

<html>
<head>
<title>highlight date</title>
<style>
.row {
background-color: Yellow;
color:blue;
}

</style>
<script type="text/javascript">

</script>
<script src="jquery-1.3.2.min.js" type="text/javascript"></script>
<script language="javascript">
var currentTime = new Date();
var day = currentTime.getDate();
var month = currentTime.getMonth() + 1;
var year = currentTime.getFullYear();
var review = day + "/" + month + "/" + year;

</script>
<script language="javascript">
$(document).ready(function() {
$('#names td:contains(xxxx)').parent().addClass('row');
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table id="names">
<tbody>
<tr>
<td>1</td>
<td>review</td>
</tr>
<tr>
<td>2</td>
<td>Satheesh</td>
</tr>
<tr>
<td>3</td>
<td>08/10/2010</td>
</tr>
<tr>
<td>4</td>
<td>11/10/2010</td>
</tr>
<tr>
<td>5</td>
<td>xyz</td>
</tr>
</tbody>
</table>
</div>
</form>
<h4>It was
<script type="text/javascript">document.write(review)</script></h4>
</body>
</html>

I get get it to highlight a set string, but for some reason when ever i try to declare the variable "review" as what i want to search for and highlight, all it seems to find is the word review. can anyone help me on this at all please? its starting to do my head in.

Thankyou

+3  A: 

You have not provided enough information.

My psychic debugging skills tell me that you need to change it to

$('#names td:contains(' + someVariable + ')')
SLaks
the variable from his code is 'review'
Luke Schafer
sorry fisrt time user, the location where i am trying to declare the variable in the code is at xxxx. when i insert a string in here, ie the word review, it searches through the table, finds a row that has a cell with the contents 'review' and adds the CSS style to the row. what i want to do is to insert a variable here and have the function search the table for this variable and add the CSS style to those rows, ie the variable of todays date.
Jason Maher
@Jason - what you have described is what both SLaks and I answered, though I spotted the actual variable, he must have missed it
Luke Schafer
A: 

I'm guessing you're trying

$('#names td:contains(review)').parent().addClass('row');

you should instead concatenate your variable:

$('#names td:contains(' + review + ')').parent().addClass('row');

which will join '#names td:contains(' to the value of your variable, followed by ')'

EDIT: I took pity. Below is my solution to the additional question you asked in the comments. Please note you should make the parseDate function more robust.

$(document).ready(function() {
    function parseDate(dateString)
    {
        //You should address this function to be more robust
        return new Date(Date.parse(dateString));
    }

    $('#names tr').each(function(index)
    {
        var row = $(this);
        if (parseDate(elem.find("td:eq(1)").text()) < new Date())
            row.addClass('row');
    });
});

This gets each row and iterates through them. It gets the textual value of the SECOND (0-index) td, and parses it to a date. If this date is less than today, it applies the class.

It DOES NOT check whether the data is an actual date or not, so you should add in that check somehow.

Luke Schafer
thankyou so much, that now works, one more quick question, how do i get it to look for dates prior to my variable, ie all dates before today?
Jason Maher
You don't, unfortunately. The date values are strings. It would be a much more involved (but possible) solution. I suggest a new question targeting that specific requirement. Also, don't forget the 'accept' the solution you find most fitting.
Luke Schafer
Thanks for all you help Luke and SLaks
Jason Maher
No worries. I also added a solution to your additional question to my answer because I was a little bored :)
Luke Schafer
thanks for taking pitty on me Luke, i've only been doing this hole programming thing now for about 9weeks and i must admit i am still shite with it. lol. so shite in fact i cant even get your last bit of coding to work for me at all. i assume that i replace my function with yours and it should work?
Jason Maher
I sensed some newbishness going on which is why I decided to help out :) Good luck with it. Yes, you should be able to just replace your jquery onready call (the `$(document).ready` part) with what I posted. I really feel like you may be treading on ground a little too complex for a 9-week newbie, so you should try to leverage from your colleagues as much as possible (and if they're decent people, they'll help you out)... assuming this is work and not play, in which case, blogs are your friend.
Luke Schafer
Thanks Luke, i knew it was prob to complex when i started out, but i thought i'd give it a go. unfortunatley i realy cant leverage off my colleagues as they have no idea, and our IT department never learnt Javascript when it first came out (yes they are that old). so thankyou for all your help. if i have any more dumb question do you mind if i ask you for some help?
Jason Maher
hehe ok then. Sure, not a problem, but you're best off doing what you did and post a question here.
Luke Schafer
i shall do. thanks heaps for your help
Jason Maher