Skip to content

Instantly share code, notes, and snippets.

@dhcole
Created March 25, 2013 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhcole/5239548 to your computer and use it in GitHub Desktop.
Save dhcole/5239548 to your computer and use it in GitHub Desktop.
Make links on page bookmarkable
// loop through the node list to get the innerHTML from each node
// copy that as the id for the node
var addId = function(list){
for (i = 0; i < list.length; i++) {
element = list[i];
sectionCopy = element.innerHTML.toLowerCase().replace(/ /g, '-').replace(/([^0-9a-z-])/g,''); //substitute space with dash
element.id = sectionCopy;
}
};
var allH3 = document.getElementsByTagName('h3');
addId(allH3);
var allH2 = document.getElementsByTagName('h2');
addId(allH2);
// make the titles linkable
$('h2[id]','.main').attr('title', 'Link to this section').click(function(e) {
// chapterName = $('h2[id]','.main')[0].id;
window.location.hash = $(e.currentTarget).attr('id');
});
$('h3[id]','.main').attr('title', 'Link to this section').click(function(e) {
// chapterName = $('h2[id]','.main')[0].id;
window.location.hash = $(e.currentTarget).attr('id');
});
// mark image captions
var caption = $('img').next().addClass('caption');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment