While using a audio plugin for wordpress, the span/class automatically adds parentheses around the text, for example:
`<span class="songwriter">(Female & Male dialog, Humorous, Storyteller, Cocky, Sassy)</span>`
Is there a way to remove the parentheses using JavaScript?
I tried:
$('span.songwriter').text(function(index, text) {
return text.replace(/(|)/g, '');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span class="songwriter">(Female & Male dialog, Humorous, Storyteller, Cocky, Sassy)</span>
This now works in the snippet and JSFiddle, but parentheses are still present on the page…https://www.cathydvoiceactor.wolfandkey.com/
3
Answers
try this:
The reason your code doesn’t work is because of a typo, and had you looked at your developer console the error should have been somewhat obvious, see below:
The reason that
text.replace
is not a function is becausetext
isn’t what you think it is; the first argument is the index of the current element in the collection returned by jQuery, and the second is the existing text:When jQuery doesn’t work as you expect it’s always worth checking the – excellent, and easily referenced – jQuery documentation to see why.
References:
String.prototype.replace()
.text()
If you can’t prevent the html from showing up on the page, you could remove the parentheses using something like this: