Disable All Links Within DIV

This JavaScript function disables all links within the specified DIV by setting the link object to ‘disabled’ as well as removing the ‘onclick’ and ‘href’ attributes. This is a useful JavaScript function for form submissions since it prevents the user from clicking another link while an action is pending.

function DisableAllDIVLinks(DivId) {
  var div = document.getElementById(DivId);
  var anchorList = div.getElementsByTagName('a');

  for(var i = 0; i < anchorList.length; i++) {
    anchorList[i].disabled = true;
    anchorList[i].onclick = '';
    anchorList[i].removeAttribute('href');
  }
}

The following is a modified version that disables all links within a specified DIV, but it retains a backup of the links so that they can be re-enabled with the EnableAllDIVLinks function.

function DisableAllDIVLinks(DivId) {
  var div = document.getElementById(DivId);
  var anchorList = div.getElementsByTagName('a');

  for(var i = 0; i < anchorList.length; i++) {
    anchorList[i].disabled = true;
    anchorList[i].setAttribute('backup_href', anchorList[i].getAttribute("href"));
    anchorList[i].removeAttribute('href');
  }
}

function EnableAllDIVLinks(DivId) {
  var div = document.getElementById(DivId);
  var anchorList = div.getElementsByTagName('a');

  for(var i = 0; i < anchorList.length; i++) {
    anchorList[i].disabled = false;
    anchorList[i].setAttribute('href', anchorList[i].getAttribute("backup_href"));
    anchorList[i].removeAttribute('backup_href');
  }
}

Round Double To Nearest Hundredth

The following snippet demonstrates several ways to round a double in Java to the nearest integer, tenths, hundredths, or thousandths decimal place. The code handles both positive and negative numbers (and zero, too).

double x = 0.0d;
double y = 0.0d;

// Double rounded to the nearest integer.
y = (int) (x + ((x < 0.0) ? -0.5 : 0.5));

// Double rounded to the nearest tenth.
y = ((int) ((x * 10.0) + ((x < 0.0) ? -0.5 : 0.5))) / 10.0;

// Double rounded to the nearest hundredth.
y = ((int) ((x * 100.0) + ((x < 0.0) ? -0.5 : 0.5))) / 100.0;

// Double rounded to the nearest thousandth.
y = ((int) ((x * 1000.0) + ((x < 0.0) ? -0.5 : 0.5))) / 1000.0;