Last active
August 29, 2015 14:14
-
-
Save mikofski/19aa3c8a7775a2af158b to your computer and use it in GitHub Desktop.
reference generator using counters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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