cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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.

            
              <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>

            
          
!
            
              * {
  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;
}
            
          
!
            
              // 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
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console