I have several headers on a page, and I need to populate a navigation box on each page that links to the appropriate anchor."
However none of my headers are anchored. I have too many pages to do this manually. can anyone come up with a clean jquery solution?
function addAnchors(){
//loop through all your headers
$.each($('h1'),function(index,value){
//append the text of your header to a list item in a div, linking to an anchor we will create on the next line
$('#box-anchors').append('<li><a href="#anchor-'+index+'">'+$(this).html()+'</a></li>');
//add an a tag to the header with a sequential name
$(this).html('<a name="anchor-'+index+'">'+$(this).html()+'</a>');
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With