JSFiddle - React, Tailwind, and code Playground
HTML
<div class="wrapper">
<p>some <a href="#" class="tag">text</a> here and more <a href="#" class="tag">text</a></p>
<p>some <a href="#" class="tag active">text</a> here</p>
<p>some <a href="#" class="tag">pizza</a> here and more <a href="#" class="tag">text</a></p>
<p>some <a href="#" class="tag">text</a> here and some more <a href="#" class="tag">text</a></p>
</div>
<div class="nav">
<a href="#" class="back">Back</a>
<a href="#" class="next">Next</a>
</div>
CSS
.active{
color:red;
font-weight:bold;
text-decoration:none;
}
JavaScript
console.clear();
$(".nav .back").on('click', function(e) {
e.preventDefault();
if(this.href) { $(".wrapper .active").triggerHandler('findPrev').click(); }
});
$(".nav .next").on('click', function(e) {
e.preventDefault();
if(this.href) { $(".wrapper .active").triggerHandler('findNext').click(); }
});
$(".nav .back, .nav .next").on('enable', function() { // <<< custom event handler
$(this).attr('href', '#');
}).on('disable', function() { // <<< custom event handler
$(this).removeAttr('href');
});
$(".tag").on('findPrev', function() { // <<< custom event handler
var $tags = $(this).closest('.wrapper').find('.tag');
var index = $tags.index(this);
return (index > 0) ? $tags.eq(index - 1) : $();
}).on('findNext', function() { // <<< custom event handler
var $tags = $(this).closest('.wrapper').find('.tag');
var index = $tags.index(this);
return (index < $tags.length) ? $tags.eq(index + 1) : $();
}).on('click', function(e) {
e.preventDefault();
$(".wrapper .tag").filter(".active").removeClass('active').end().filter(this).addClass('active'); // move the 'active' highlight
$(".nav .back").trigger($(this).triggerHandler('findPrev').length ? 'enable' : 'disable'); // manage the back button
$(".nav .next").trigger($(this).triggerHandler('findNext').length ? 'enable' : 'disable'); // manage the next button
// desired click action here
}).filter(".active").trigger('click'); // trigger 'click' to initialize everything