Show Menu
Cheatography

javascript Cheat Sheet (DRAFT) by [deleted]

This is a draft cheat sheet. It is a work in progress and is not finished yet.

lifo push

function lifo(){


document.getElementsByName("push")[0].addEventListener('click', lifo_push);
document.getElementsByName("pop")[0].addEventListener('click', lifo_pop);
document.getElementsByName("peek")[0].addEventListener('click', lifo_peek);

function lifo_push(){
  var val = document.getElementById("newItem").value;
  var ul = document.getElementById("lifo");

  var li = document.createElement("li");
  li.appendChild(document.createTextNode(val)); //Ajoute val à <li>

  ul.insertBefore(li,ul.childNodes[0]);
  document.getElementById("newItem").value= "";
};

function lifo_pop(){
  var ul = document.getElementById("lifo");
  var taille = ul.getElementsByTagName("li").length; //Get the number of <li>
  if(taille>0){
    //On retire le premier element
    ul.getElementsByTagName("li")[0].remove();
  }
  else{
    //Error
    alert("Aucun élément");
  }
};

function lifo_peek(){
  var ul = document.getElementById("lifo");
  var taille = ul.getElementsByTagName("li").length;
  if(taille>0){
    var div = document.getElementById('peek_area');
    var val = ul.getElementsByTagName("li")[0].innerHTML;
    div.innerHTML = val;
  }
  else{
    alert("Aucun élément");
  }
};

}
window.addEventListener("load",lifo,false);
 

exercice 32 misenplace


function miseEnPlace(){
var taille = document.getElementsByTagName("P").length;
var i = 0;
document.getElementById('b1').addEventListener('click', function () {
  document.body.style.backgroundColor = "yellow";
});

document.getElementById('b2').addEventListener('click', function () {
  var h1 = document.getElementsByTagName("h1")[0];
  if(h1.style.color=="red"){
    h1.style.color="#66f";
  }
  else{
    h1.style.color="red";
  }
});

document.getElementById('b3').addEventListener('click', function () {
 if(i<5){
  document.getElementsByTagName("P")[i].style.fontStyle="italic";
  i++;
}
});

}
window.addEventListener("load",miseEnPlace,false);