This is my html:
<div class="names my-3 text-center">
<a href="#" class="anchor">Susan</a>
<a href="#" class="anchor">Jack</a>
<a href="#" class="anchor">Bill</a>
</div>
<br />
<h5 class="currentname">Jack</h5>
One of the anchors that contains the same name as class currentname should have class red with it.
var currentname = $('.currentname').text(); // read currentname in h5
$('.names a').text(name).addClass('red');
what i am doing wrong? ( Now they all get red)
Use contains and add class depends upon that.
var currentname = $('.currentname').text(); // read currentname in h5
$("a.anchor:contains(" + currentname + ")").addClass('red');
.red {
color: red
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="names my-3 text-center">
<a href="#" class="anchor">Susan</a>
<a href="#" class="anchor">Jack</a>
<a href="#" class="anchor">Bill</a>
</div>
<br />
<h5 class="currentname">Jack</h5>
One of the anchors that contains the same name as class currentname should have class red with it.