user2417624 user2417624 - 6 months ago 6
Javascript Question

How to add html tags before and after certain string using jquery

I have the following HTML:

<h5 class="pi-accordion-title">
<a href="#" class="z">
<span class="pi-accordion-toggle"></span>
My Link (Keyword)
</a>
</h5>


which is part of an accordion link. What I am trying to do is to open span tag before the (Keyword) and close it after that.

I am trying with the following JAVASCRIPT:

<script>
$("(Keyword)").before("<span class='orange'>");
$("(Keyword)").after("</span>");
</script>


but it doesn't work...Console log show the following error:Syntax error, unrecognized expression: (Keyword)

Answer
$('.pi-accordion-title').each(function() {
    this.innerHTML = this.innerHTML.replace(/(\(Keyword\))/, '<span class="orange">$1</span>');
});
Comments