Differences between revisions 3 and 4
Revision 3 as of 2009-07-13 16:02:08
Size: 742
Editor: CarlNobile
Comment:
Revision 4 as of 2009-07-13 16:03:27
Size: 790
Editor: CarlNobile
Comment:
Deletions are marked like this. Additions are marked like this.
Line 6: Line 6:
  var result = null;
Line 7: Line 9:
    return this.insertBefore(newNode, refNode.nextSibling);     result = this.insertBefore(newNode, refNode.nextSibling);
Line 9: Line 11:
    return this.appendChild(newNode);     result = this.appendChild(newNode);
Line 11: Line 13:

  return result;

JavaScript Helper Functions

When you cannot use a JavaScript toolkit like jQuery these functions may come in handy.

Node.prototype.insertAfter = function(newNode, refNode) {
  var result = null;

  if(refNode.nextSibling) {
    result = this.insertBefore(newNode, refNode.nextSibling);
  } else {
    result = this.appendChild(newNode);
  }

  return result;
}

Node.prototype.getElementByClass = function(className, count) {
  var result = null;

  for(var i = 0; i < this.childNodes.length; i++) {
    var classCount = 0;

    if(this.childNodes[i].className == className) {
      if(classCount == count) {
        result = this.childNodes[i];
        break;
      }

      classCount++
    }
  }

  return result;
}

JavaScriptHelperFunctions (last edited 2009-07-17 20:58:15 by CarlNobile)