LOADING...

Preview

Pen ID
Unlock Campus Themeforest adv

 

Code



  
  Document



  
  

Access By ID

  • jQuery
  • JavaScript
CSS
html {
  box-sizing: border-box;
}

*,
*:before,
*:after {
  box-sizing: inherit;
}

body {
  font-family: "Alegreya Sans SC", "Helvetica Neue";
}

strong {
  font-weight: 500;
}

h2 {
  border-top: 2px solid #1c1c1c;
  padding-top: .25em;
  margin-bottom: 1em;
  display: inline-block;
  font-weight: 300;
}

ul li {
  display: inline-block;
}

.s-container {
  width: 90%;
  margin: 3em auto auto;
}

@media(min-width:800px) {
  .s-container {
    max-width: 650px;
  }
}

#myBoxJS,
#myBoxjQ {
  width: 100px;
  height: 100px;
  border: 1px solid #f2f2f2;
  border-radius: .25em;
  text-align: center;
  cursor: pointer;
}

#myBoxJS span,
#myBoxjQ span {
  line-height: 100px;
  font-weight: 600;
}

.bg-red {
  background: darkred;
}

#listContainer {
  margin: 0 auto;
  display: block;
  float: none;
  width: 203px;
}
JS
// jQuery — this works, but it's not resusable
/*var myBox = $("#myBoxjQ");
$(myBox).on("click", function() {
  $(this).toggleClass("bg-red");
});
*/
$(document).ready(function() {

  // jQuery
  var myBox = $("#myBoxjQ");
  // this is modular 
  function toggleMe() {
    $(this).toggleClass("bg-red");
  }

  $(myBox).on('click', toggleMe);

});

// Vanilla JavaScript 
var myBox = document.querySelector("#myBoxJS").addEventListener("click", function() {

  this.classList.toggle('bg-red');
});

Description

A comparison between toggling classes via ID selection in both jQuery and raw Javascript.
Term
Sat, 04/14/2018 - 20:42

Related Codes

Pen ID
Pen ID
Pen ID
Square Adv