function DisableLinks(className) {
var links = document.getElementsByTagName('A'),
linksLen = links.length,
link,
i;
for (i = 0; i < linksLen; i += 1) {
link = links[i];
if (link.className === className) {
disableLink(link);
}
}
}
function EnableLinks(className) {
var links = document.getElementsByTagName('A'),
linksLen = links.length,
link,
i;
for (i = 0; i < linksLen; i += 1) {
link = links[i];
if (link.className === className) {
enableLink(link);
}
}
}
function disableLink(el) {
el.onclick = function () {
return false;
}
}
function enableLink(el) {
el.onclick = function () {
return true;
}
}
There are all sorts of ways to improve this script, so that multiple class names on an emelent can be catered for, or to reduce the amount of duplication throughout the script, but this is the basic starting point from where further refinements can be made.
One example of such an improvement is to move the looping out to a separate function.
function DisableLinks(className) {
var links = document.getElementsByTagName('A');
forEachClass(className, links, disableLink);
}
function EnableLinks(className) {
var links = document.getElementsByTagName('A');
forEachClass(className, links, enableLink);
}
function disableLink(el) {
el.onclick = function () {
return false;
}
}
function enableLink(el) {
el.onclick = function () {
return true;
}
}
function forEachClass(className, els, fn) {
var elsLen = els.length,
el,
i;
for (i = 0; i < elsLen; i += 1) {
el = els[i];
if (el.className === className) {
fn(el);
}
}
}