Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Document Method-2 : createAttribute</title>
</head>
<body>
  <p id="sports"></p>
</body>
</html>
 
var js = {
  lineNumber: 1
};
js.log = function(text) {
  var node, child, lineText;
  node = document.getElementById('result');
  if (!node) {
    node = document.createElement('div');
    node.id = 'result';
    document.body.appendChild(node);
  }
  child = document.createElement('div');
  lineText = js.lineNumber + '.';
  lineText = Array.isArray(text) ? lineText + '[' + text + ']' : lineText + text;
  if (arguments.length > 1) {
    for (var k = 1; k < arguments.length; k++) {
      lineText += arguments[k];
    }
  }
  child.innerText === undefined ? child.textContent = lineText : child.innerText = lineText;
  node.appendChild(child);
  js.lineNumber += 1;
  return this;
};
///////////////////////////////////////////////////////
window.onload = function() {
  var node = document.createAttribute('sportsNode');
  node.nodeValue = "Created Node";
  var el = document.getElementById('sports');
  el.setAttributeNode(node);
  js.log('nodeValue: ' + el.getAttribute('sportsNode'));
};
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x