webmonkey237
webmonkey237

Reputation: 379

jQuery - Remove certain characters from string

I have a database inside a custom CMS out-putting plain HTML data.

One of the columns of data has for example the string 2003-07-01T00:00:00.000

I'm trying to remove T00:00:00.000 just leaving the date 2003-07-01.

My attempt is as follows:

HTML

<span class="dataIssue">2003-07-01T00:00:00.000</span>

jQuery

jQuery(".dataIssue").text().replace('T00:00:00.000','');

Possible reasons why I think this could be failing:

My script is wrong.

It takes awhile for the database to load so maybe the script is loading before the database has finished populating on the page (over 3000 records)

Thanks

Upvotes: 1

Views: 10504

Answers (2)

Fancyoung
Fancyoung

Reputation: 2423

jQuery(".dataIssue").html(jQuery(".dataIssue").text().replace('T00:00:00.000',''));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<span class="dataIssue">2003-07-01T00:00:00.000</span>

Upvotes: 0

epascarello
epascarello

Reputation: 207511

You are replacing the string, you need to apply the change back to the element. And if you have multiple elements, you need to loop over each one.

jQuery(".dataIssue").each( function () {
    var elem = $(this);
    var txt = elem.text().replace('T00:00:00.000','');
    elm.text(txt);
});

Upvotes: 3

Related Questions