Quelltext der Datei: http://www.michaelster.ch/jsTestFolder/appendTextChild.htm

Dateigrösse: 1.56 kb

[Anzeige ohne Zeilennummern]


1 
<!DOCTYPE html>
<html lang="de">
<head>
  <meta charset="utf-8">
  <title>DOM - appendChild</title>

<script>
// from: https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array
// With ES2015 you can use this one:
Array.prototype.shuffle = function() {
  let m = this.length, i;
  while (m) {
    i = (Math.random() * m--) >>> 0;
    [this[m], this[i]] = [this[i], this[m]]
  }
  return this;
}
var a = [1, 2, 3, 4, 5, 6, 7];
var r = a.shuffle();
console.log(r);
</script>

</head>
 
<body>

<h2>Headline</h2>


<button id="check">check var b</button>
<p id="ausgabe"></p>


<script>

/*** http://xahlee.info/js/js_textContent_innerHTML_innerText_nodeValue.html ***/
/* the most compatible way to change a element's text content 
   the script has to be after the requested element, here h2 */
var myElem = document.getElementsByTagName("h2")[0];
var newTxt = "Lovecats";

(function(ele, text){
    if ( ele.firstChild ) {
        ele.firstChild.nodeValue = text;
    } else {
        ele.appendChild(document.createTextNode(text));
    }
})( myElem, newTxt );

var b;
var button = document.getElementById('check'); 
    button.addEventListener('click', check);
var ausgabe = document.getElementById('ausgabe');

function check(){
  console.log('pop '+ b +' oop');
  if(b) {
    ausgabe.innerHTML = 'true: Variable b is: '+b;
  } else {
    ausgabe.innerHTML = 'false: Variable b is: '+!!b; // ohne !! is undefined
  }
  b = b ? false : true;
}
</script>

</body>
</html>