css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <header id="page-header">
   <span for="navbar-checkbox" class="navbar-handle">&equiv;</span>
  <span class="right-span">jQuery to Vanilla JS</span>
 
</header>
<nav id="navbar" class="main-nav">
  <!-- hidden header just to pass test but not useful in my actual creation   -->
  <header style="display: none"></header>

   <ul>
    <a class="nav-link" href="#why_learn_plain_js" rel="internal"><li>Why learn plain JS</li></a>
     <a class="nav-link" href="#add_class"><li class="tae">Add Class</li></a>
     <a class="nav-link" href="#get_html"><li>Get HTML</li></a>
     <a class="nav-link" href="#get_text"><li>Get Text</li></a>
     <a class="nav-link" href="#get_style"><li>Get Style</li></a>
     <a class="nav-link" href="#resources"><li>Resources</li></a>
  </ul>
</nav>
<main id="main-doc">
  <section class="main-section" id="why_learn_plain_js">
    <header><h2>Why learn plain JS</h2></header>
    <article>  
      <p>Common JavaScript functions for a website can nowadays be easily done in plain JS. It's ok to use jQuery and we use it to be efficient most of the time but I think that you must understand that you can do everything without the framework and libraries. There is much power in learning plain Javascript. You will understand any new frameworks and  you will be a complete developer. Also, jQuery syntax is typically slightly shorter and that also means downloading and parsing a huge library.  </p>
      </artice>
    </section>
  
  <section class="main-section" id="add_class">
    <header><h2>Add Class</h2></header>
    <article>
      <p><strong>JQUERY</strong></p>
       <pre><code>
$(el).addClass(className);       
       </code></pre>
              
        <p><strong>IE8+</strong></p>
       <pre><code>
if (el.classList)
  el.classList.add(className);
else
  el.className += ' ' + className;
     </code></pre>      
    </article>
    </section>
  <section class="main-section" id="get_html">
    <header><h2>Get HTML</h2></header>
    <article> 
<p><strong>JQUERY</strong></p>
       <pre><code>
$(el).html();       
       </code></pre>
              
        <p><strong>IE8+</strong></p>
       <pre><code>
el.innerHTML
       </code></pre>    
    </article>
    </section>
  <section class="main-section" id="get_text">
    <header><h2>Get Text</h2></header>
    <p><strong>JQUERY</strong></p>
       <pre><code>
$(el).text();      
       </code></pre>
              
        <p><strong>IE8+</strong></p>
       <pre><code>
el.textContent
     </code></pre>    
    </section>
  <section class="main-section" id="get_style">
    <header><h2>Get Style</h2></header>
    <p><strong>JQUERY</strong></p>    
       <pre><code>
$(el).css(property);      
       </code></pre>
              
        <p><strong>IE9+</strong></p>
       <pre><code>
getComputedStyle(el)[property];
     </code></pre> 
    </article>
    </section>
<section class="main-section" id="resources">
    <header><h2>Resources</h2></header>
    <p><strong>Resources for learning vanilla (plain) Javascript versions of  jQuery methods.</strong></p>    
     <ul>
       <li><a href="https://www.jamestease.co.uk/blether/vanilla-js-equivalent-jquery-find-parent-data-remove-class-empty-append#classes">Resource 1</a></li>
       <li><a href="https://callmenick.com/dev/jquery-functions-javascript-equivalents/">Resource 2</a></li>
       <li><a href="http://youmightnotneedjquery.com/">Resource 3</a></li>
       <li><a href="https://plainjs.com/javascript/">Resource 4</a></li>
       <li><a href="https://vanillajstoolkit.com/code-snippets/">Resource 5</a></li>
     </ul>
    </article>
    </section>
 
</main>



<!--  
  You can find reference for the ff(see below) here...

  TODO:
    1. offsetting an html anchor to adjust for fixed header ...
    2. Responsive menu bar

  design reference: https://developers.google.com/style/

-->
            
          
!
            
              @import url('https://fonts.googleapis.com/css?family=Lato:400,700|Open+Sans:400,700');

/* BASE STYLES */
* {
  box-sizing: border-box;
  padding: 0;
  margin: 0;
}

html,body{  
	color: #414a4e;
	font-family: Lato;    
  line-height: 1.5;
  background: #eceff1;
}

h1, h2, h3, h4, h5, h6,
p, li {
  margin: 0;
  font-family: 'Lato';
}

h2,h5 { font-weight: 700; }
h1{ font-size: 32px; }
h2{ font-size: 24px; }
h3,h4{ font-size: 18px; }
h5{ font-size: 14px; }
p{ font-size: 16px; line-height: 24px; }

/* Custom styles */

#page-header {
  background: #1976D2; 
  color: #fff;
  height: 40px;
  line-height: 40px;
  width: 100%;
  text-align: right;
  padding-right: 10px;
  padding-left: 10px;
  z-index: 9;
  
/*   sticky header */
  position: fixed;
  top: 0; 
  
  display: flex;
  justify-content: space-between;
  overflow: hidden;  
}

#navbar {
  font-size: 13px;
  background: #fff;
  max-height: 200px;
  overflow-x: hidden;
  overflow-y: auto;
  display: none;
  top: 40px; /*  height of header */
  position: fixed;
  width: 100%;
  padding: 16px;
  
  
}

#navbar.nav-menu-open{
  display: block;
}

.navbar-handle {
  cursor: pointer;
  font-size: 24px;
}

#navbar ul {
  padding: 0;
  line-height: 16px;
}

#navbar li {
  list-style: none;     
  margin-top: 8px;
  line-height: 16px; 
  color: #212121;
}

#navbar a {
  text-decoration: none;
}

#navbar li:hover {
  color: #039be5;
} 

#main-doc {
  margin-top: 80px; /*height of header + space */
  padding: 0 20px;  
}

section h2 {
  border-bottom: 0;  
  letter-spacing: 0;
  margin: 48px 0 24px;
  padding-bottom: 0; 
  font-weight: 700;
}

/* offsetting an html anchor to adjust for fixed header */
section:target:before {
  content:'';
  display:block;  
  height:1px; /* just so the h2 margin-top will have effect */
}

pre { 
  margin: 5px 0;
  padding-left: 5px;
  background: #f7f7f7; 
 }
code{
  font-family: Courier;
  font-size: 14px;
  line-height: 1.5;
}
ul li {
  list-style: none;
  margin-top: 5px;
}

#page-header .right-span {
    display: none;
  }

@media (min-width: 320px) {
   #page-header .right-span {
    display: block;
  }
}


@media (min-width: 800px) {
    
  #navbar {
    display: block;
    width: 230px; 
    max-height: 100vh;
    height: 100%;  
  }
  
  #main-doc {
    position: absolute;
    left: 230px; /*navbar width*/
    margin-top: 10px;
  }
  
  .navbar-handle { display: none; }
}



            
          
!
            
              
let navbar = (function(){  
  
  const mainNavBar = document.getElementById("navbar");
  const navBarToggle = document.getElementsByClassName('navbar-handle')[0];
  const breakpoint = 800; //800px viewport and below, show the hamburger menu
  
  //on small devices, toggle menu on hamburger menu and 'X' icon click
  navBarToggle.addEventListener('click', toggleNavMenu);

  //on small devices, hide menu on menu item click
  document.querySelector('#navbar ul').addEventListener('click', function(e){ 
    if( e.target.tagName.toLowerCase() === 'li'  ) { toggleNavMenu(); }      
  });

  //hide menu when escape key is clicked
  document.addEventListener('keyup', escapeKeyPressed )

  function toggleNavMenu(e){     
    
    if(!isScreenSmall) return; //no need for wide screens where the dropdown navbar doesn't exist

    if(mainNavBar.classList.contains('nav-menu-open') === false) {     
        mainNavBar.className += " nav-menu-open";
    } else {
        mainNavBar.className = "main-nav";       
    }

    updateNavMenuBarIcon();
  }
  
  function escapeKeyPressed(e , x) { 
      
     if(!isScreenSmall) return; 
    
     if(e.key === 'Escape' ){
      
      let isMenuOpen = mainNavBar.classList.contains('nav-menu-open'); 
      
      if( isMenuOpen ) {        
          mainNavBar.classList.toggle('nav-menu-open', false);
          updateNavMenuBarIcon();        
      }
    }   
  }

  //set menu icon to hamburger/open or 'x'/close
  function updateNavMenuBarIcon(){
    
    if(!isScreenSmall) return; //no need for wide screens where the dropdown navbar doesn't exist
    
    let symbol = (mainNavBar.classList.contains('nav-menu-open')) ? 'X' : '&equiv;';

    document.querySelector('.navbar-handle').innerHTML = symbol;
  }
  
  //if screen
  function isScreenSmall() {
    return document.documentElement.clientWidth < breakpoint;
  }
  
})();


            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console