Reputation: 32828
I have the following code that checks for click events on links inside of the element with id = menu:
$("#menu")
.on('click', 'a[href^="/City"]', function (event) {
event.preventDefault();
$('#editLink').attr("data-href", link);
});
The HTML that this code works on looks like this:
<ul id="menu">
<li><a href="/City/0101004H">1</a></li>
<li><a href="/City/0101004I">2</a></li>
<li><a href="/City/0101004J">3</a></li>
<li><a href="/City/0101004K">4</a></li>
</ul>
I have two links on another part of my screen as below:
<a id="prev" href="#">Prev</a>
<a id="next" href="#">Next</a>
How can I make it so that if a user clicks on the "2" link then these links are changed to:
<a id="prev" href="/City/0101004H" title="City 1">Prev</a>
<a id="next" href="/City/0101004J" title="City 3">Next</a>
Upvotes: 0
Views: 2996
Reputation: 20323
You can use do like :
$("#menu").on('click', 'a[href^="/City"]', function (event) {
event.preventDefault();
if(jQuery(this).text() == 2) { // this check is not required if this functionality is required for any 'a' under #menu
jQuery(this).closest('ul').find('li:first').find('a').css('color','yellow');
jQuery(this).closest('ul').find('li:last').find('a').css('color','pink');
$('#prev').prop('href', $(this).parent().prev().find('a').prop('href')).prop('title','City 1').css('color','red');
$('#next').prop('href', $(this).parent().next().find('a').prop('href')).prop('title','City 3').css('color','green');
}
});
Update:
jQuery(this).parent().prev().find('a') // this will give prev a
jQuery(this).parent().prev().find('a') // this will give next a
Upvotes: 2