I want to use jQuery to find a link and hide it . I want to search for the text of the link using jQuery. This doesn't work:
<a href="example.com/foo-bar">a website link</a>
function replace_text() {
var linkText = jQuery('a').text();
var answer = linkText.replace('a website link', '');
}
replace_text();
To hide the element by it's text you could use the :contains
selector, then hide()
, like this:
$('a:contains("a website link")').hide();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="example.com/foo-bar">a website link</a>
Note that the above is a case-insensitive greedy match. If you want an exact match you can use filter()
, like this:
$('a').filter(function() {
return $(this).text().trim() == 'a website link';
}).hide();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="example.com/foo-bar">a website link</a>