Skip to content

Instantly share code, notes, and snippets.

@mikofski
Last active August 29, 2015 14:14
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 mikofski/19aa3c8a7775a2af158b to your computer and use it in GitHub Desktop.
Save mikofski/19aa3c8a7775a2af158b to your computer and use it in GitHub Desktop.
reference generator using counters
/* Build a references section that lists URLs of hyperlinks
* with specified class "ref". Put list in div element with
* specified id "references". Use CSS class called references
* that uses counters and ::before psuedo-element to add
* brackets around reference list numbers.
* links in body look like this:
* blah <a href="URL>reference</a><a class="ref"><a> */
document.body.onload = gen_refs("a", "references", "ref");
function gen_refs(tagname, refsname, classname) {
var alltags = document.getElementsByTagName(tagname);
var refs = document.getElementById(refsname);
var content = '<h2 class="references">References</h2>';
var ctr = 0
for (var i = 0; i < alltags.length; i++) {
var tag_class = alltags[i].getAttribute("class");
if (tag_class == classname) {
console.log(alltags[i]);
content += '<p class="references"><a href="';
content += alltags[i].previousSibling.getAttribute("href");
content += '">' + alltags[i].previousSibling.getAttribute("href") + '</a></p>';
alltags[i].setAttribute("href", "#references");
alltags[i].innerHTML = '<b>[' + ++ctr + ']</b>';
}
}
console.log(content);
refs.innerHTML = content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment