I want to make a function that remove any specific word in the content
I got this code
jQuery
<script>
$(document).ready(function(){
$('#cl').click(function(){
$('div').remove(':contains("kamal")');
});
})
</script>
HTML
<div>asdjh alsdhj kamal ljkahsdhasd lakshd kamal</div>
<div ><a href="#" id="cl">click</a></div>
<div>asdjh alsdhj ljkahsdhasd lakshd </div>
but its remove whole div contains kamal I want to remove only this word from my content not the whole div you can also see online demo of my code here
The correct way to do a read-modify-write on an element is to use jQuery’s "function parameter" methods:
this avoids calling
.text()twice, and also simplifies the code logic.Note that you may get unusual results if you have nested
divtags, since the:contains()pseudo selector considers all descendants, not just direct children, and does it top-down rather than bottom-up. This is why the above solution includes the initial.filtercall, to ensure that only leaf nodes in the DOM tree are considered.An alternative method is to use
.contentsand look directly at DOM text nodes:See https://jsfiddle.net/alnitak/eVUd3/
EDIT second example updated to use
string.match(regex)instead ofregex.test(string).