Aariba Aariba - 1 month ago 6
HTML Question

split specific text from element by simple script

Trying to split specific text from element. Given js it working, how to make short/simple this js?



Element html example:

<div id="ReviewBox">
<span>(By:) Hello</span>

<div id="ReviewBox">
<span>By: Goodbye</span>
</div>

</div>


JS:

$('#ReviewBox span:contains("By:")').each(function(){
$(this).html($(this).html().split("(").join(""));
$(this).html($(this).html().split(")").join(""));
});


Please Jsfiddle: http://jsfiddle.net/xSphc/60/


How to make short/simple this js?

Answer

You can use .html(function), also you should use .replace() with regular expression to remove ( and )

$('#ReviewBox span:contains("By:")').html(function(_, html) {
  return html.replace(/[/(\)]/g, '');
});

$('#ReviewBox span:contains("By:")').html(function(_, html) {
  return html.replace(/[/(\)]/g, '');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="ReviewBox">
  <span>(By:) Hello</span>
</div>

Fiddle