0

I have a piece of web-hosted software in which I need to expose some sort of sorting value at the start of my string to make the items appear in the correct order. The software will automatically sort the values alphabetically so I've calculated a sorting value and the outputs now sort nicely by age order like this:

  • 001 Years Old David
  • 033 Years Old Annabelle
  • 055 Years Old Carol
  • 065 Years Old Bill

I want to allow the software to sort the values using these values but then I would like the browser to only actually display the name of the person:

  • David
  • Annabelle
  • Carol
  • Bill

I am wondering if I could write a script in JQuery that listens for the text that contains " Years Old " and then strips the first 14 characters out of the display, e.g. something like

$(".DIV:contains(' Years Old ')").html(
  //Here I would like to show only the text after the first 14 characters
);

If anyone has any ideas on how to achieve the above or has a better idea for how to achieve it then I would really appreciate the input!

Mukyuu
  • 6,436
  • 8
  • 40
  • 59
B.I.
  • 111
  • 8
  • 1
    To solve this, you do NOT have the number in the content but in a data attribute and sort on that. Alternatively put it into a span and hide that – mplungjan Dec 04 '19 at 09:32
  • 3
    Why not just don't put the years part into the HTML at all, rather than having to change it afterwards? – CertainPerformance Dec 04 '19 at 09:32
  • you can use https://stackoverflow.com/questions/21423210/how-to-use-substr-function-in-jquery `substr` – Nirav Joshi Dec 04 '19 at 09:32
  • you can use the substring function of javascript to get the required the string and display but then you might have to change the code accordingly. – Nilesh Jain Dec 04 '19 at 09:33
  • @CertainPerformance because the software (TIBCO Spotfire) can only sort alphabetically by the value that is in the string. I cannot access the other data attributes in the table (e.g. age) – B.I. Dec 04 '19 at 09:47
  • So my suggestion using a span and hide it should work – mplungjan Dec 04 '19 at 10:17

4 Answers4

3

To remove the first 14 characters from the div text you can provide a function to text() which accepts the current text value as an argument. Then you can use substring() to remove the first N characters and return the result to update the element. Try this:

$('div').text((i, t) => t.substring(14));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>001 Years Old David</div>
<div>033 Years Old Annabelle</div>
<div>055 Years Old Carol</div>
<div>065 Years Old Bill</div>

However it's worth noting that this is not an optimal solution. A better approach, assuming it's possible, would be for you to place the ordering index in to a data attribute on the div. Then you can simply order by that and not have to worry about amending the text afterwards. Something like this:

$('#container div').sort((a, b)  => $(a).data('order') < $(b).data('order') ? -1 : 1).appendTo('#container');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="container">
  <div data-order="65">Bill</div>
  <div data-order="55">Carol</div>
  <div data-order="1">David</div>
  <div data-order="33">Annabelle</div>
</div>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • I have no access to the rest of the data table via JS on this piece of software so I cannot order by a different column. Thanks a lot for the tip though. The first answer you gave works really well though so I really appreciate it. – B.I. Dec 04 '19 at 11:29
1

You can use use split() with pop().

$('div').text((index, value) => value.split(' ').pop());
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>001 Years Old David</div>
<div>033 Years Old Annabelle</div>
<div>055 Years Old Carol</div>
<div>065 Years Old Bill</div>

Alternative use Regex.

$('div').each(function(i, v) {
  let strings = $(this).text();
  let regex = /^((\S+)\s){3}(.+)/gi;
  let match = regex.exec(strings);
  $(this).text(match[3]);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>001 Years Old David</div>
<div>033 Years Old Annabelle</div>
<div>055 Years Old Carol</div>
<div>065 Years Old Bill</div>
4b0
  • 21,981
  • 30
  • 95
  • 142
0

idk what app that you using for,

if in web ex.html

substring(0, 14);
substring(start, end);

hope answer your question

Sidik
  • 128
  • 11
0

Version using span

$('ul li')
  .detach()
  .sort((a, b) =>  $(a).text() < $(b).text() ? -1 : 1)
  .appendTo('ul');
.age { display:none }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
  <li><span class="age">055</span>Carol</li>
  <li><span class="age">001</span>David</li>
  <li><span class="age">065</span>Bill</li>
  <li><span class="age">033</span>Annabelle</li>
</ul>

Version using data attr

$('ul li')
  .detach()
  .sort((a, b) =>  $(a).data("age") < $(b).data("age") ? -1 : 1)
  .appendTo('ul');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
  <li data-age="055">Carol</li>
  <li data-age="001">David</li>
  <li data-age="065">Bill</li>
  <li data-age="033">Annabelle</li>
</ul>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • Thanks for the feedback. In the piece of software that I am using, there is no way for the JS to access data from other fields in the source tables. Therefore, it's not possible to append a "data-age" property. For your first suggestion, I had tried to add tags around the text when I created the calculated value (e.g. "
    **** 001 Years Old **** David
    ") but this was just being displayed in the text rather than being interpreted by the DOM. I really appreciate the feedback.
    – B.I. Dec 04 '19 at 11:45
  • If you update innerText it will show the tags. You need to update innerHTML to have the span work – mplungjan Dec 04 '19 at 13:32