user1382306
user1382306

Reputation:

Sort unparsable tablesorter date column by unix timestamp

I need to sort a jQuery tablesorter by a unix timestamp rather than an unpredictable displayed date that depends upon locale, so I want to try a possible solution where a hidden span can be used to sort the date column.

How can an element be inserted in front of text or at the beginning of another element while retaining the existing text contents in the containing element?

Basic intent

Given this element:

<td id="myTD">Difficult date to parse</td>

I'd like to turn it into this:

<td id="myTD">
    <span style="display:none">1398019663</span>
    Difficult date to parse
</td>

Upvotes: 0

Views: 249

Answers (2)

Joseph
Joseph

Reputation: 924

You could store the value in a temporary variable, then append that value into whatever you need to insert.

JSfiddle for example: http://jsfiddle.net/kWzwF/2/

e.g:

var myVal = $('h1').text()
$('h1').html('<span>newstuff </span>' + myVal);

Upvotes: 0

Zoyd
Zoyd

Reputation: 3559

This looks like a job for HTML5 data attributes, as in :

<td id="myTD" data-sortkey="1398019663">
    Difficult date to parse
</td>

You can get and set the value of data attributes using the data function in JQuery.

Upvotes: 2

Related Questions