Jquery.parents ()
I have a list and a link, I want to change the top link:
<a class="topLink" href="#">change me</a>
<ul>
<li><a href="#">link name</a></li>
<li><a href="#">link name</a></li>
<li><a href="#">link name</a></li>
<li><a href="#">link name</a></li>
<li><a href="#">link name</a></li>
</ul>
When I click on the link name link, I want this text to apply to the link to me.
How to do this with jQuery parents? or what should i use?
Edit: Sorry, I forgot to say that I will have three of these lists, so the three links that have the topLink class
+3
6 answers
If <ul>they <a>are neighboring siblings, then trythis:
$('ul').delegate('a','click',function() {
$(this).closest('ul').prev().text( $(this).text() );
});
It uses the jQuery methodclosest() to get the first ancestor <ul>and jQuery.prev() to go from <ul>the previous brother.
HTML , , HTML.
+1
$('ul').delegate('a', 'click', function() {
$('.topLink').text($(this).text());
});
.parents() ( )
.delegate() , ul. , , ul id.
Ref.: . delegate()
:
+3