Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
93
rated 0 times [  95] [ 2]  / answers: 1 / hits: 20946  / 14 Years ago, thu, october 7, 2010, 12:00:00

I have a nav, each link in the nav just appends a hashtag on the existing URL when clicked, for live filtering, etc. with the use of jQuery.



I want to append the SAME current hashtag to a series of links within a div further down the page.



For example, I've clicked work and my URL now looks like:



http://www.domain.com/page.html#work


I have a series of links in the page:



<div id=links>
<ul>
<li><a href=http://www.domain.com/link1>Link1</a></li>
<li><a href=http://www.domain.com/link2>Link2</a></li>
<li><a href=http://www.domain.com/link3>Link3</a></li>
<li><a href=http://www.domain.com/link4>Link4</a></li>
</ul>
</div>


These links within the div#links need to be updated on the fly to append #work on all the URL's so that when clicked the hashtag is appended.



Is this possible? Does this make sense?


More From » jquery

 Answers
43

You should attach a click event handler for links in #nav and change links in #links accordingly. [See it in action]



Javascript



$(#nav a).click(function() {
$(#links a).each(function() {
this.href = this.href.split(#)[0] + # + window.location.hash;
});
});​


HTML



<div id=nav>  
<a href=#work>work</a> -
<a href=#school>school</a>
</div>

[#95391] Tuesday, October 5, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
deanna

Total Points: 84
Total Questions: 86
Total Answers: 107

Location: Cyprus
Member since Wed, Dec 8, 2021
3 Years ago
;