omer schleifer
omer schleifer

Reputation: 3935

Change html in children

I have a html document which goes to create excel report. Here is a link to sample html in jsfiddle:

http://jsfiddle.net/UrM7k/

I wish to edit the html content so that instead of two lines for each time period I will have one line,
seperated by " - ". Here is some code I've tried:

var fromTime = $(div).find("td.date_range");
    //replace new line between dates with "-" sepeartor
    var replaced = fromTime.html().replace(/<br\s?\/?>/, " - ");
    //fromTime.html(replaced);

it does remove the extra line,
but it changes all of the periods to be the same like the first.

Would appreicate any help. Thanks, Omer

Upvotes: 1

Views: 63

Answers (3)

Jim Ade
Jim Ade

Reputation: 71

fromTime is the entire set of cells, but you are only calling replace() once on it. I suspect you will need to iterate over that collection, and do the replace() call for each one separately.

Upvotes: 1

jgroenen
jgroenen

Reputation: 1326

$(div).find("td.date_range").each(function() {
    var replaced = $(this).html().replace(/<br\s?\/?>/, " - ");
    $(this).html(replaced);
});

Upvotes: 2

Jason P
Jason P

Reputation: 27012

You're close. You could use each() to iterate and this to reference the current item:

http://jsfiddle.net/4v3sE/

$("td.date_range").each(function() {
    var $this = $(this);
    $this.html($this.html().replace(/<br\s?\/?>/, " - "));
});

But I think this solution is cleaner:

http://jsfiddle.net/wfYnH/

$("td.date_range br").replaceWith(' - ');

Upvotes: 3

Related Questions