<span id="subscription-toggle" class="list:subscription">
| <!-- I want to surround this with a div with a class -->
<span class="" id="subscribe-77" style="">
<a class="dim:subscription-toggle:subscribe-77:is-subscribed" href="http://localhost/taiwantalk2/topic/topic-with-two-tags-topic-with-two-tags-topic-with-two-tags/?action=bbp_subscribe&topic_id=77&_wpnonce=25988e5dac">Subscribe</a>
</span>
</span>
So this should be the final result:
<span id="subscription-toggle" class="list:subscription">
<div class="hide-this"> | </div>
<span class="" id="subscribe-77" style="">
<a class="dim:subscription-toggle:subscribe-77:is-subscribed" href="http://localhost/taiwantalk2/topic/topic-with-two-tags-topic-with-two-tags-topic-with-two-tags/?action=bbp_subscribe&topic_id=77&_wpnonce=25988e5dac">Subscribe</a>
</span>
</span>
jQuery text() Method The text() method sets or returns the text content of the selected elements. When this method is used to return content, it returns the text content of all matched elements (HTML markup will be removed). When this method is used to set content, it overwrites the content of ALL matched elements.
Answer: Use the jQuery text() method You can simply use the jQuery text() method to get all the text content inside an element. The text() method also return the text content of child elements.
If the text node you wish to wrap your div
around is the only text node (i.e. it's the only text inside subscription-toggle
that is not inside another element) then you can do this:
$("#subscription-toggle").contents().filter(function() {
return this.nodeType == 3;
}).wrap("<div class='hide-this'></div>");
You can see an example here.
Alternatively, if there are multiple text nodes in the span
, and you only want to wrap |
characters, replace the body of the filter
function with this line:
return $(this).text().indexOf("|") != -1;
You can see an example of that one working here.
I haven't tested but it should work:
var subsc = $('#subscription-toggle');
subsc.html(subsc.html().replace('/( \| )/g', '<div class="hide-this">$1</div>'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With