I'm trying to create a navigation menu using jQuery with links to each <h2>
title on pages created and published in Wordpress. Some of these pages can get a bit lengthy so this navigation menu should make it quick and easy for visitors to jump to certain parts of it's content.
I've got it set up so that each <h2>
title creates it's own unique ID by using it's content prefixed by "#" (i.e. "My title" would get the ID "#My title") using this piece of code below:
jQuery("h2").each(function(){
var id = jQuery(this).html();
jQuery(this).attr("id", "#" + id);
})
Basically step 1 of 10.. What's left is to create a function that loops trough each <h2>
that exists on the page and then creates a menu item for each of those titles. I kind of understand javascript loops but I'm just not sure how to get this to actually create new elements like this.
I'm still a novice when it comes down to Javascript or even jQuery so I apologise if anything's unclear.
Anyways I hope somebody can point me in the right direction. Any help would be much appreciated.