I've this code :
<div>
Hello world.
<a href="#">test</a>
<a href="#">login</a>
</div>
I want to replace "hello world." With "Hello everybody !".
I try this :
$('div').text('Hello everybody !');
But it does not work, thanks for your help !
Try this :
$.fn.myfilter = function(textToReplaceWith)
{
$(this).contents().filter(function() {
if(this.nodeType === 3 && $.trim($(this).text()) != "") // nodeType = 3 represents textual content in an element
$(this).replaceWith(textToReplaceWith);
});
}
$('div').myfilter('Hello everybody !');
Example : https://jsfiddle.net/DinoMyte/6d5ry9br/8/
You may also pass params to replace a specific text dynamically.
$.fn.myfilter = function(textToReplace,textToReplaceWith)
{
$(this).contents().filter(function() {
if(this.nodeType === 3 && $.trim($(this).text()) == textToReplace)
$(this).replaceWith(textToReplaceWith);
});
}
$('div').myfilter('Hello world.','Hello everybody !');
$('div > span')