Pen Settings

HTML

CSS

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

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <header>
  <h1>Article search</h1>
  
  <form id="search" method="get">
    <input id="q" name="q" type="search" placeholder="search..." />
    <button type="submit">Go</button>
  </form>
</header>

<main>
  <h2>Results</h2>
  <ol id="results">
  </ol>
  <p id="none">No results found.</p>
  <p id="running">Running query...</p>
</main>

              
            
!

CSS

              
                * {
  padding: 0;
  margin: 0;
}

body {
  font-family: sans-serif;
  font-size: 100%;
  color: #333;
  background-color: #fff;
  max-width: 40em;
  padding: 0 2em;
  margin: 1em auto;
}

main {
  margin: 2em 0;
}

ol {
  margin: 1em 0 2em 2em;
}

li {
  margin-top: 0.3em;
}
              
            
!

JS

              
                // restdb.io query handler
var restDB = (function() {

  var 
    api = 'https://sitepoint-fbbf.restdb.io/rest/',
    APIkey = '597dd2c7a63f5e835a5df8c4';
  
  function query(url, callback) {
    
    var timeout, xhr = new XMLHttpRequest();
    xhr.open('GET', api + url);
    xhr.setRequestHeader('x-apikey', APIkey);
    xhr.setRequestHeader('content-type', 'application/json');
    xhr.setRequestHeader('cache-control', 'no-cache');
    
    // response
    xhr.onreadystatechange = function() {
			if (xhr.readyState !== 4) return;
			var err = (xhr.status !== 200), data = null;
      clearTimeout(timeout);
			if (!err) {
				try {
					data = JSON.parse(xhr.response);
				}
				catch(e) {
          err = true;
					data = xhr.response || null;
				}
			}
			callback(err, data);
		};
    
    // timeout
    timeout = setTimeout(function() {
      xhr.abort();
      callback(true, null);
    }, 10000);
    
    xhr.send();
    
  }

  return {
    query: query
  };

})();

// main application
(function() {
  
  var 
    form = id('search'),
    q = id('q'),
    results = id('results'),
    none = id('none'),
    running = id('running');
  
  show(none);
  
  // form submit event
  form.addEventListener('submit', function(e) {
    
    // stop submit
    e.preventDefault();
    
    // get query
    var qv = q.value.replace(/\s+/g, ' ').trim();
    if (!qv) return;
    
    // run query
    show(running);
    
    // run query
    restDB.query(
      '/content?' + 
      'q={ "$or": [' +
      '{ "title": {"$regex": "' + qv + '"} }, ' + 
      '{ "body": {"$regex": "' + qv + '"} }' + 
      ']}&' +           
      'h={ "$fields": { "slug": 1, "title": 1, "published": 1 } }',
    showResult);
    
  });
  
  
  // show search results
  function showResult(err, data) {
    
    if (err || !data || !data.length) {
      // error or no results
      show(none);
    }
    else {
      // empty results
      while (results.lastChild) results.removeChild(results.lastChild);
      
      // add results to list
      var r, page, li, a;
      for (r = 0; r < data.length; r++) {
        
        page = data[r];
        
        a = document.createElement('a');
        a.href = 'https://www-sitepoint-fbbf.restdb.io/' + page.slug;
        a.textContent = page.title + (page.published ? '' : ' [draft]');
        
        li = document.createElement('li');
        li.appendChild(a);
        
        results.appendChild(li);
      }
      
      show(results);
      
    }
    
  }
  
  // get element
  function id(id) {
    return document.getElementById(id);
  }
  
  // show and hide nodes
  function show(node) {
    s(results);
    s(none);
    s(running);

    function s(item) {
      item.style.display = (node === item ? 'block' : 'none');
    }
  }
  
})();

              
            
!
999px

Console