0

I'm trying to compare two dates in different formats to highlight new items added from last visit but there is one problem with first date:

jQuery( "td.details > span:last-child" ).each(function() {
  var a = jQuery( this ).text();
  var b = a.split('/');
  var c = b[1]+'/'+e[0]+'/'+e[2]; //dd.mm.yyyy=>mm.dd.yyyy
  var d = new Date( c );
  var date = d.getTime();

    jQuery('<span/>', {
    'class':'date',
    'text': date,
    }) .appendTo( "td.details > p" );
});

HTML:

    <td class="details"><p>new item1</p><span>10/1/2017 17:06</span></td>
    <td class="details"><p>new item2</p><span>09/1/2017 22:26</span></td>
    <td class="details"><p>new item3</p><span>09/1/2017 11:18</span></td>
    <td class="details"><p>new item4</p><span>08/1/2017 14:32</span></td>

Every td have different date, expectation is in every td will be created span with appropriate d.getTime() value (just testing), but newly created span contains all d.getTime() values (from every td) not only that one. What am I doing wrong?

Second date will be same(lastvisit) for every td so no problem

chazsolo
  • 7,873
  • 1
  • 20
  • 44
Nidecker
  • 172
  • 8
  • `.appendTo( "td.details > p" );` says, "append to all p in all td.details". You need to scope this to the current one. At a guess, this might work (hence comment not answer): .`appendTo($(this).closest("td").find(".details > p"));`. – freedomn-m Jan 10 '17 at 17:30
  • did not work, but `.appendTo(this)` works great – Nidecker Jan 10 '17 at 17:40

1 Answers1

0

Will this work better?

jQuery('<span/>', {
  'class':'date',
  'text': date,
}).after(this);

In your code you are appending date to every td.details > p not only the current one. .after() will add the after the your last span inside td

strah
  • 6,702
  • 4
  • 33
  • 45