1

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)

3
  • Why don't you put it in a span in the first place? Commented Jun 3, 2016 at 2:00
  • WIth your code you will have to update the innerHTML of the anchor a Commented Jun 3, 2016 at 2:01
  • @progrAmmar I don't want to add html code in my database. My link is dynamically pulled from the database. And I have different words in (Keyword) so I will place span with different classes depending from the word. Commented Jun 3, 2016 at 2:06

3 Answers 3

1
$('.pi-accordion-title').each(function() {
    this.innerHTML = this.innerHTML.replace(/(\(Keyword\))/, '<span class="orange">$1</span>');
});
Sign up to request clarification or add additional context in comments.

3 Comments

Strange, but no span has been inserted...should I use body on load or something?
Yes, I can see that here, but it doesn't work on my computer, I will restart it, I can't see any other obvious reason why this is not working on my side.
"should I use body on load or something" is most likely true.
1

You cannot select text like this with jQuery. Here is a very simple way to do it which does not even need jQuery.

The drawback of this approach is that the entire content of the container is re-written, which means that if you want to attach event to some children of the container, you have to do it after you inserted the spans. As a result, I would recommend to choose a container as small as possible.

var toReplace = '(Keyword)'; var containers = document.getElementsByClassName("pi-accordion-title"); Array.prototype.forEach.call(containers, function(container){ container.innerHTML = container.innerHTML.replace(toReplace, '$&'); });

.pi-accordion-title a span {
  color:red;
}
<h5 class="pi-accordion-title">
    <a href="#" class="z">
        <span class="pi-accordion-toggle"></span>
            My Link (Keyword)
    </a>
</h5>

EDIT

If you want to avoid the aforementioned element overwriting drawback, you can leverage the ":contains" jQuery selector. If you are using jQuery I actually think it is the best/safest method as you will only re-write the parent elements.

var toReplace = '(Keyword)';
$(":contains(" + toReplace + ")").each(function(){
  this.innerHTML = this.innerHTML.replace(toReplace, '<span>$&</span>');
});
.pi-accordion-title a span {
  color:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h5 class="pi-accordion-title">
    <a href="#" class="z">
        <span class="pi-accordion-toggle"></span>
            My Link (Keyword)
    </a>
</h5>

5 Comments

This code produces an error which is : TypeError: container is null
You should be using an old/odd browser that oddly does not support document.body. Here is a new version using getElementsByClassName. If your browser does not support this, I cannot help you :).
I don't know what is wrong, but no span is inseted before or after (Keyword) in my browser. I check it with Firefox, Chrome and Opera, nothing...all my browsers are up to date
Try to wrap your code in a $(function(){ \* code here *\ }); wrapper (or window.addEventListener("load", function(){ \* code here *\ });). This problem would also explain the document.body === null problem.
You need to make sure that your page is loaded before attempting any operation on your DOM. If your script is inserted at the beginning of the page (e.g. the header), it is executed before the page is loaded.
0

I have tried with jquery.Grab the class name and get the text or html ,then you can replace by .replace with find word

var title = $(".pi-accordion-title");
var text = title.html();
title.html(text.replace(/(\(Keyword\))/g,"<span class='orange'>$&</span>"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.