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.

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

              
                <div class="container center">
      <div class="page-header panel-primary">
        <h1>Wikipedia viewer</h1>
      </div>
      <p class="lead">
        Select a random Wikipedia article or search for an article on your favorite topic.
      </p>
      <div class="row">
        <div class="col-sm-4">
          <div id="random"><button class="btn btn-primary" type="button"><i class="fa fa-random"></i> Show a random article</button></div>
        </div>
        <div class="col-sm-8">
          <div class="input-group">
            <input id="search-str" type="text" class="form-control" placeholder="Search for...">
            <span class="input-group-btn">
              <button id="search-btn" class="btn btn-primary" type="button"><i class="glyphicon glyphicon-search"></i></button>
            </span>
            </div><!-- /input-group -->
          </div>
        </div>
        <div class="row">
          <div class="col-sm-12">
            <div id="search-results">            
            </div>
          </div>
        </div>
      </div>
      <div class="footer">
        <div class="container center">
          <p class="text-muted">&copy; hepabolu 2016</p>
          <p class="text-muted">
            Created for <a href="https://www.freecodecamp.com">FreeCodeCamp</a><br/>
            Icons from <a href="http://fontawesome.io">Font Awsome</a> and <a href="http://glyphicons.com/">
          Glyphicons</a>
        </p>
      </div>
    </div>

              
            
!

CSS

              
                 @import url("https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css");
      @import url("https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css");

      body > .container {
        padding: 60px 15px 0 15px;
      }

      .footer {
        margin-top: 60px;
        /*background-color: #f5f5f5;*/
        padding: 15px;
        width: 100%;
      }
      
      .footer a {  
        color: #222;
      }
      
      .center {
        float: none;
        margin-left: auto;
        margin-right: auto;
      }

      h1 {
        color: #337ab7;
      }

      #random {
        padding-bottom: 15px;  
      }

      #search-results {
        margin-top: 30px;
      }

      #search-results p.info,
      #search-results p.warning {
        color: #337ab7;
        font-size: 2em;
        font-weight: bold;
        width: 50%;
        margin: auto;
      }

      #search-results p.warning {
        color: #f0ad4e;
      }

      .article {
        clear: left;
        border: 1px solid #f5f5f5;
        height: 80px;
        margin-bottom: 15px;
      }

      .article:hover {
        background-color: #f5f5f5;
      }

      .article a {
        text-decoration: none;
        color: #337ab7;
      }

      .article header {
        font-size: 1.25em;
        font-weight: bold;
      }

      .article p {
        color: #000;
      }

      .article img {
        height: 80px;
        width: 80px;
        float: left;
        margin: 0 14px 0 0;
        object-fit: contain;
        object-position: top;
      }
              
            
!

JS

              
                $("#random").on("click", function() {
  getRandomArticle();
});

$("#search-btn").on("click", function() {
  var searchString = $("#search-str").val();

  getTopics(searchString);
});

$("#search-str").keypress(function(e) {
  var key = e.which;
  if (key == 13) {
    var searchString = $("#search-str").val();
    getTopics(searchString);
  }
});

//
// getRandomArticle
//

function getRandomArticle() {
  var url = "https://en.wikipedia.org/wiki/Special:Random";

  window.open(url, '_blank');
}

//
// getTopics
//

function getTopics(searchStr) {
  console.log('searching for ', searchStr);

  if (searchStr === undefined || searchStr === '') {
    console.log('nothing to search for');
    var html = '<p class="warning">Please enter search text</p>'
    $("#search-results").html(html);
    return;
  }

  var baseUrl = "https://en.wikipedia.org/w/api.php";

  // console.log('searching for ' + encodeURIComponent(searchStr));

  var params = {
    action: "query",
    formatversion: 2,
    format: "json",
    generator: "prefixsearch",
    gpssearch: "'" + searchStr + "'",
    gpslimit: 10,
    prop: "pageimages|pageterms|pageprops",
    piprop: "thumbnail",
    pithumbsize: "80",
    pilimit: "10",
    redirects: '',
    wbptterms: "description"
  };

  $.ajax({
      url: baseUrl,
      jsonp: "callback",
      dataType: "jsonp",
      data: params,
      success: function(response) {
        showSearchResults(response);
      },
      error: function(arguments) {
        // TODO handle gracefully
      }
  });

  function showSearchResults(obj) {
    // console.log(obj);

    var html = '';
    var description = '';
    var thumb = '';
    var title = '';
    var url = '';


    if (obj.batchcomplete && ! obj.hasOwnProperty("query")) {
      html = "<p class='info'>No results found</p>";
    } else {
      for (var i = 0; i < obj.query.pages.length; i++) {
        var article = obj.query.pages[i];

        description = '';
        thumb = '';
        title = article.title;
        url = 'https://en.wikipedia.org/wiki/' + encodeURIComponent(title);
        // console.log(i, " parsing ", article);

        if (article.terms !== undefined) {
          description = "<p>" + article.terms.description + "</p>";
        } 

        if (article.thumbnail !== undefined) {
          thumb = '<img class="responsive-img valign" src="' + article.thumbnail.source + '"/>';
        }
        else {
          thumb = '<img src="https://www.wikipedia.org/portal/wikipedia.org/assets/img/noimage.png"/>';
        }

        html += '<div class="article">' +
        '<a href="' + url + '" target="_blank">' + 
        thumb +
        '<header>' + title + '</header>' +
        '<p>' + description + '</p>' +
         '</a>' +
        '</div>';
      }      
    }

    $("#search-results").html(html);

  }
}
              
            
!
999px

Console