remove % and keep testing using jQuery
<div id="top_content">testing %</div> == 0$
I’ve tried to use this code but it didn’t work
$('.top_content:contains("%: ")').each(function(){
$(this).html($(this).html().split("%: ").join(""));
});
top content is an element inside wordpress plugin
thanks for help
4
Answers
$('#top_content').(...);
for select element by id$(...).replace(...);
method for rename or delete textLike this:
The two reasons your original code didn’t work is, first: because you’re selecting an element incorrectly; the element has an
id
attribute but you’re using class-based selector (.top_content
); and also because you’re trying to split the string of text on a character-sequence –"%: "
– that isn’t present in that string.While this approach can certainly work:
It’s generally better to use the
replace()
method to modify text, as it replaces the string in one place, instead of having to remember to remove (withsplit()
) and thenjoin()
them together with the desired character.Below is an approach that uses
String.prototype.replace()
along with jQuery:Or, without jQuery:
References:
String.prototype.replace()
.String.prototype.split()
.String.prototype.trim()
.text()
.You’ve used the wrong selector for id remove ‘.’ and put ‘#’ for selecting the id. And since id selector just returns the element, no whole array of element, hence you cannot use loop function (.each).
Otherwise everything you have done is in the right way.
$('#top_content')
. top_content is a id namecontains()
andsplit()
function