Posted in jquery
162
1:07 am, July 9, 2021

append a #link to the end of all href urls on a target

good for if you need to append target links with a # link

append a #link to the end of all href urls on a target Demo

View Demo Full Screen View Demo New Tab

append a #link to the end of all href urls on a target Code

HTML

<h2 id="my-anchor">Bunch of Links</h2>
<ol class="bunch-of-links">
<li><a href="https://kruxor.com/view/code/mONdX/">Link One</a></li>
<li><a href="https://kruxor.com/view/code/mONdX/">Link Two</a></li>
<li><a href="https://kruxor.com/view/code/mONdX/">Link Three</a></li>
</ol>

Javascript

$(document).ready(function(){
	var qs = '#my-anchor';
	$('.bunch-of-links a').each(function() {
	    var href = $(this).attr('href');
	    if (href) {
	        href += (href.match(/\?/) ? '&' : '?') + qs;
	        $(this).attr('href', href);
	    }
	});
});

Related Tags

No Items Found.

Add Comment
Type in a Nick Name here
 
Related Search Terms