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">
  <h1 class="text-center">Search Wikipedia</h1>
  <div id="searchBox">
    <form id="search">
      <div class="col-xs-8 pull-left">
        <input type="text" id="searchTerm" name="searchTerm" class="form-control" placeholder="Enter search term" autocomplete="off"/>
      </div>
      <button type="submit" title="Search" id="doSearch" class="btn btn-info">
      <span class="fa fa-search"></span>
    </button>
      <a href="https://en.wikipedia.org/wiki/Special:Random" target="_blank" id="randomArticle" class="btn btn-warning" title="Go to a Random Article">
      <span class="fa fa-random"></span>
    </a>
    </form>
  </div>

  <div id="result">
  </div>
</div>
              
            
!

CSS

              
                
body{
  margin: 10% auto auto auto;
  background: #999;
}
#searchBox{
  width: 30%;
  margin: auto;
}
.panel a{
  text-decoration: none;
  color: #000;
}
              
            
!

JS

              
                var searchTerm,
  API_url = "https://en.wikipedia.org/w/api.php?";

var wiki_url = "https://en.wikipedia.org/wiki/";

var parameters = {
  action: "query",
  format: "json",
  prop: "extracts",
  exsentences: "3",
  exintro: "1",
  explaintext: "1",
  generator: "search",
  exlimit: "10",
  exintro: "1",
  explaintext: "1",
  gsrnamespace: "0",
  gsrlimit: "10"
};

var articleTemplate =
  '<div class="panel animated fadeInUp">' +
  '<a href="{{link}}" target="_blank">' +
  '<div class="panel-heading">' +
  '<div class="panel-title">{{title}}</div>' +
  '<div class="panel-body">{{body}}</div>' +
  "</a>" +
  "</div>";

function runQuery() {
  $("#searchTerm").focus();
  parameters.titles = searchTerm;
  parameters.gsrsearch = searchTerm;
  build_URL();
  retrieve_data();
}

function cleanUI() {
  $("#result").empty();
  $("body").css("margin", "2% auto auto auto");
  $("h1").hide();
}
function resetUI(){
  $('body').css('margin', '10% auto auto auto');
  $('h1').fadeIn();
}

function build_URL() {
  API_url += $.param(parameters);
}

function retrieve_data() {
  console.log(API_url);

  $.ajax({
    type: "GET",
    url: API_url,
    dataType: "jsonp",
    // I feel very stupid; after spending about 4 hours on this, and pretty much debugging 
    // https://codepen.io/andreibuiza/pen/mEbmKR line by line, trying to figure out
    // why THAT one works, but mine didn't, even though it was the same code.
    // Turned out I missed "T" in "dataType" (it was lowercase), causing a CORS error
    // on Wikipedia
    success: function(data) {
      //console.log(data);
      display(data);
    },
    error: function() {
      $('#result').html("<div class=\"text-danger\">Error: Could not load Wikipedia list</div>");
      console.log("Search request fail");
    }
  });
}

function display(data) {
  var prop_list = Object.getOwnPropertyNames(data.query.pages);

  //display each page entry
  prop_list.forEach(function(pages) {
    data.query.pages[pages].link_url = wiki_url + data.query.pages[pages].title;
    var thisResult = articleTemplate.replace(
      /{{title}}/g,
      data.query.pages[pages].title
    );
    thisResult = thisResult.replace(
      /{{body}}/g,
      data.query.pages[pages].extract
    );
    thisResult = thisResult.replace(
      /{{link}}/g,
      data.query.pages[pages].link_url
    );

    $("#result").append(thisResult);
    //console.log(data.query.pages[pages]);
  });
}

$(document).ready(function() {
  $("#searchTerm").focus();
  // on submit, show our results
 
  $("#search").on("submit", function() {
    event.preventDefault();

    searchTerm = $('#searchTerm').val();
    //console.log('Search Term: '+searchTerm);
    
    $("#result").empty();
    // a bit of styling
    cleanUI();
    if (searchTerm.length > 0)
      runQuery();
    else{
      resetUI();
      $('#result').html("<div class='text-center animated fadeInUp'>You searched for nothing - you got nothing :( </div>");
    }
  });
});

              
            
!
999px

Console