Ahmad Alfy
Ahmad Alfy

Reputation: 13371

Remove part of text within P tag after a " " separator

My Markup is like this :

<div class="article">
 <p class="date">16-05-2011 15:28:24</p>
</div>

This output is generated by a CMS and I searched for everywhere to modify the output but I didn't reach a solution.

I want to use jQuery to remove the "hour" from the date paragraph.

Saying I don't want any text after the "space" so that the day-month-year will show up only.

Any idea how to achieve this using jQuery?

Upvotes: 0

Views: 1353

Answers (2)

Fosco
Fosco

Reputation: 38526

Pretty easy loop, splitting by a space with a limit of 1 item:

$('p.date').each(function() {
    $(this).html($(this).html().split(" ",1)[0]);
});

Upvotes: 4

Joshua - Pendo
Joshua - Pendo

Reputation: 4371

<script type="text/javascript">
  var date = $('p.date').html(); // get date content
  var split = date.split(" "); // explode by space
  $('div.date').html(split[0]); // setting the html with the first piece
</script>

not tested, but I think this should be it. If there are more then 1 div, try this:

<script type="text/javascript">
  $(document).ready(function() {

    $('p.date').each(function(index) {
      var date = $(this).html();
      var split = date.split(' ');
      $(this).html(split[0]);
    });

  });
</script>

Upvotes: 2

Related Questions