Reputation: 3935
I have a html document which goes to create excel report. Here is a link to sample html in jsfiddle:
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
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
Reputation: 1326
$(div).find("td.date_range").each(function() {
var replaced = $(this).html().replace(/<br\s?\/?>/, " - ");
$(this).html(replaced);
});
Upvotes: 2
Reputation: 27012
You're close. You could use each()
to iterate and this
to reference the current item:
$("td.date_range").each(function() {
var $this = $(this);
$this.html($this.html().replace(/<br\s?\/?>/, " - "));
});
But I think this solution is cleaner:
$("td.date_range br").replaceWith(' - ');
Upvotes: 3