Diggetydog Diggetydog - 2 months ago 8
jQuery Question

How do I remove a string of text inside a div after a br with jquery/javascript?

I have been trying to remove variable produced pricing (from a generated list) that comes after a
in each div. Here it is:

<div class="p-name">
<a href="somelink.html" class="TLink">Product Name<br>$1.91</a>
</div>
<div class="p-name">
<a href="somelink2.html" class="TLink">Product Name2<br>$55.91</a>
</div>


I have tried:

$('.p-name').nextAll('br').remove();


and

$(".p-name").nextAll().remove();


The price can be anywhere from $0.00 to $40,000.00

Any help? Should I be using Javascript instead?

Answer

Another alternative is to reset the contents of each a by using substr

$(".p-name a").each(function() {
  var content = $(this).html();
  $(this).html(content.substr(0,content.indexOf('<br>')));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="p-name">
  <a href="somelink.html" class="TLink">Product Name<br>$1.91</a>
</div>
<div class="p-name">
  <a href="somelink2.html" class="TLink">Product Name2<br>$55.91</a>
</div>

Comments