I have the Jquery that i have put inside the <head>
:
$('#change_text').text(function(_, text) {
return text.replace(/\[.*?\]/g, '');
});
Which removes everything within the []
brackets.
And the html:
<div id="change_text">Hello [username]</div>
And that all works fine on removing [username]
But I also have the Jquery:
$('#change_text').click(function(){
$(this).html("Hey [username2]")
});
But when you click it doesn't remove the [username2]
Here is a jsfidle
$.text()
does not track your element. Try wrapping it into function and call it anytime you change html.
function changetext(){
$('#change_text').text(function(_, text) {
return text.replace(/\[.*?\]/g, '');
});
}
$('#change_text').click(function(){
$(this).html("Hey [username2]");
changetext();
});
changetext();
check fiddle
function changeText(text){
return text.replace(/\[.*?\]/g, '');
}
$('#change_text').text(function(_, text) {
return text.replace(/\[.*?\]/g, '');
});
$('#change_text').click(function(){
$(this).html(changeText("Hey [username2]"))
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.