Aamir Shahzad
Aamir Shahzad

Reputation: 6834

How remove a specific string from an element's text using jQuery?

I want to remove 12:00:00 AM from following string.

<div class="modal-body">
    <p>1/11/2016 12:00:00 AM - 1/13/2016 12:00:00 AM</p>
</div>

The results should be:

1/11/2016 - 1/13/2016

How can I remove just the 12:00:00 AM from complete string all occurrences of it.

$('.modal-body > p').text().remove("12:00:00 AM");

Upvotes: 1

Views: 3027

Answers (1)

Josh Crozier
Josh Crozier

Reputation: 240868

You could use the .replace() method:

Example Here

$('.modal-body > p').text(function () {
    return $(this).text().replace(/12:00:00 AM/g, '');
});

Result:

<div class="modal-body">
    <p>1/11/2016  - 1/13/2016</p>
</div>

If you only want to replace the first occurrence, just remove the g flag.

Upvotes: 7

Related Questions