views:

61

answers:

4

I have several tds, with ids = row[0], row[2] row[4] and so on.

<td id="row[4]">02:45</td>
<td id="row[6]">03:45</td>

The content in these are times like 03:45, 04:45 which I want to change to 03:15, 04:15 etc. using jQuery.

EDIT:

I ended up with this:

 jQuery('td[id^="row"]').each(function(){
    min = parseInt(jQuery(this).text().substr(3,2)) + 30;
    min %= 60;
    new_time = jQuery(this).text().substr(0,3) + min;
    jQuery(this).text(new_time);
  });

Is there a neater way to do this now ?

+3  A: 

You can use starts with selector ^ like this:

$('td[id^="row"]').each(function(){
   $(this).text('your text...');
});

The code above goes over each td whose id starts with row.

More Info:

Sarfraz
formatting messed up.. please look at answer below.
Shikher
A: 

It depends on how you are accessing the cells. They can simply be named serially if they don't hint about their content at all. Assuming you've got that taken care of, the syntax for changing a (correctly named) element is

$('#your_id').html('text or HTML string here');

or

$('#your_id').text('text or HTML string here');

to replace only the text therein.

Isaac Lubow
+1  A: 

Another solution.. Since you only want to change the text inside the td s, try this...

$('#myTable td').text( function (i, oldValue) {
    return oldValue.replace('45', '15');
});

You could also use the selector suggested by @Sarfaz instead.

Untested, but I believe it should work.

Shrikant Sharat
A: 

Thanks for your response... I ended up with this... Is there a neater way to do it now ? I wanna make 01:00 to 01:30, 01:15 to 01:45, 01:30 to 01:00 and 01:45 to 01:15

 jQuery('td[id^="row"]').each(function(){
    min = parseInt(jQuery(this).text().substr(3,2)) + 30;
    min %= 60;
    new_time = jQuery(this).text().substr(0,3) + min;
    jQuery(this).text(new_time);
  });
Shikher
I think you should have added this to your question (by editing) instead of posting it as an answer. (Since it has another question in it).
Shrikant Sharat
Thanks... Did that...
Shikher