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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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.

Editor Settings

Code Indentation

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

Visit your global Editor Settings.

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.

HTML

            
              <main class="container">

  <header class="main-header">Wikipedia Search</header>

  <a href="http://en.wikipedia.org/wiki/Special:Random" target="_blank" class="btn-random">Random article</a>

  <form class="search-form">
    <input type="text" class="search-form__input" id="search" placeholder="Search" autocomplete="off">
  </form>

  <div class="img-container">
    <img srcset="https://res.cloudinary.com/eeeps/image/upload/f_auto,q_70,w_256/mountains_kwzql7.jpg 256w,
                https://res.cloudinary.com/ddy54k4ks/image/upload/f_auto,q_70,w_512/mountains_kwzql7.jpg 512w,
                https://res.cloudinary.com/ddy54k4ks/image/upload/f_auto,q_70,w_768/mountains_kwzql7.jpg 768w,
                https://res.cloudinary.com/ddy54k4ks/image/upload/f_auto,q_70,w_1024/mountains_kwzql7.jpg 1024w,
                https://res.cloudinary.com/ddy54k4ks/image/upload/f_auto,q_70,w_1280/mountains_kwzql7.jpg 1280w" src="https://res.cloudinary.com/ddy54k4ks/image/upload/f_auto,q_70,w_512/mountains_kwzql7.jpg" alt="pretty mountains" />

  </div>

  <div class="search-results"></div>

</main>
            
          
!

CSS

            
              $dark-gray: #303841;
$mid-gray: #3A4750;
$teal: #00ADB5;
$light-gray: #EEEEEE;

$default-margin: .8em;

html {
  color: $light-gray;
  box-sizing: border-box;
}

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

body {
  width: 100vw;
  font-family: 'Nunito', sans-serif;
  font-size: 16px;
  background-color: $dark-gray;
}

img {
  max-width: 100%;
}

main {
  width: 80%;
  margin: 2em auto;
}

a {
  color: $dark-gray;
  text-decoration: none;
}

.main-header {
  font-size: 2em;
  color: $light-gray;
  text-align: center;
  margin: 0 0 $default-margin 0;
}

.btn-random {
  color: $light-gray;
  background-color: $teal;
  text-decoration: none;
  padding: .5em;
  width: 100%;
  display: block;
  text-align: center;
  margin: 0;
}

.search-form__input {
  width: 100%;
  padding: .5em;
  font-family: inherit;
  font-size: inherit;
  background-color: $light-gray;
  border: none;
  margin: $default-margin 0;
}

.img-container {
  transition: all .3s;
  max-height: 500px;
  overflow:hidden;
}

.img-container.animated {
  opacity: 0;
  max-height: 0;
}

%box {
  color: $dark-gray;
  display: block;
  margin: 1em 0;
  padding: 1em;
  background-color: $light-gray;
  border: 4px transparent solid;
  transition: border .2s;
}

.search-results {
  
  &__header {
    font-size: 1.5em;
    margin: 0 0 $default-margin 0;
    color: $light-gray;
  }
  
  &__article {
    @extend %box;
    
    &:hover {
      border: 4px $teal solid;
    }
  }
}

.article__header {
  margin: 0 0 .5em 0;
  color: $mid-gray;
  font-weight: bold;
  font-size: 1.15em;
}

.searching,
.error {
  @extend %box;
}
            
          
!

JS

            
              const $form = $("form");
const $resultsHolder = $(".search-results");
const $main = $("main");
const $searchInput = $("#search");
const DEFAULT_ERR_MSG = "Oops! Something went wrong. A refresh of the page might help.";

function getData(searchString) {
  return $.ajax({
    url: `https://en.wikipedia.org/w/api.php?action=query&list=search&format=json&srsearch=${searchString}&srlimit=10`,
    dataType: "jsonp"
  });
}

function getPageIds(titles) {
  return $.ajax({
    url: `https://en.wikipedia.org/w/api.php?action=query&format=json&indexpageids=&titles=${titles.join("|")}&redirects=`,
    dataType: "jsonp"
  });
}

function resultTemplate(o) {
  return `<a href="https://en.wikipedia.org/?curid=${o.id}" target="_blank" class="box">
            <article class="search-results__article">
              <header class="article__header">${o.title}</header>
              <p>${o.snippet}</p>
            </article>
           </a>`;
}

function updateView(err, results) {
  
  $(".img-container").addClass("animated");
  $(".searching").remove();
  
  if (err) {
    showError(err);
    return;
  }

  let html = "<header class=\"search-results__header\">Search results</header>";

  results.forEach(result => {
    html = html.concat(resultTemplate(result));
  });
  
  $resultsHolder.html(html);
}

function findId(o, title) {
  for (let prop in o) {
    if (o[prop].title === title) {
      return prop;
    }
  }
  return null;
}

function showError(msg) {
  $main.append(`<div class=\"error\">${msg}</div>`);
}

function initSearch(searchStr) {

  $main.append("<div class=\"searching\">Searching...</div>");

  getData(searchStr).done(data => {

    if (data.query.search.length === 0) {
      updateView("Sorry, no results were found.");
      return;
    }

    let titles = data.query.search.map(result => result.title);

    getPageIds(titles).done(pageData => {

      let results = data.query.search.map(result => {
        return {
          title: result.title,
          snippet: result.snippet,
          id: findId(pageData.query.pages, result.title)
        };
      });

      updateView(null, results);
    }).fail(function() {
      updateView(DEFAULT_ERR_MSG);
    });

  }).fail(function() {
    updateView(DEFAULT_ERR_MSG);
  });
}

function handleForm(e) {
  e.preventDefault();
  $searchInput.blur();
  initSearch($searchInput.val());
}

$form.on("submit", handleForm);
            
          
!
999px

Console