user2417624 user2417624 - 1 year ago 81
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)

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:

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

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

Answer Source
$('.pi-accordion-title').each(function() {
    this.innerHTML = this.innerHTML.replace(/(\(Keyword\))/, '<span class="orange">$1</span>');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download