123

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

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.

            
              <div class = "container">
  <div class = "row">
    <div id="search-menu" class = "col-md-12 col-s-12 col-xs-12 absolute-center search-menu">
      <h1>The Wikipedia Viewer</h1>
      
      <div class="search-tools">
        <div class="search-box">
          <form id="search-form" class="search-form">
            <input id="search-input" class="form-control" type="text" placeholder="Keywords to search">
            <btn id="search-btn" class ="btn btn-link search-btn"><i class="fa fa-search" aria-hidden="false"></i></btn>
           
          </form>
        </div>
        
        <a class ="btn random-btn" href="https://en.wikipedia.org/wiki/Special:Random" target="_blank"><i class="fa fa-magic fa-lg random-icon" aria-hidden="false"></i>Random Article</a>
        
      </div>
    </div>
  </div>
  <div class = "row container">
    <div id = "search-results" class = "col-md-12 col-s-12 col-xs-12">
      
      <h1 id ="search-results-heading">The Wikipedia Viewer</h1>
      <div id = "search-results-toolbar">
        <p id = "search-label" class = "pull-left"></p>
        <a id = "new-search-btn" class = "pull-right btn" href="#"><i class="fa fa-fw fa-lightbulb-o" aria-hidden="false"></i>New Search</a>
      </div>
      <h2 id = "search-results-placeholder" class="text-center"> Retrieving results...</h2>      
      </div>
    </div>
  </div>
</div>
            
          
!
            
              body {
  color: black;
  background-colour: white;
  font-family: 'Roboto Condensed', sans-serif;
}

h1 {
  font-family: 'Merriweather', serif;
  font-size: 350%;
  margin-bottom: 1em;
}

h2 {
  font-size: 250%;
}

.search-menu {
  
}

.absolute-center {
  width: 50%;
  width: fit-content;
  
  height: 50%;
  height: fit-content;
  
  text-align: center;
  
  position: absolute;
  margin: auto;
  top: 0; left: 0; bottom: 0; right: 0; /* Set the bounds in which to center it, relative to its parent/container */
  padding: 3em;
}

.search-box {
  display: inline-block;
  width: 100%;
  border-radius: 3px;
  padding: 4px 55px 4px 15px;
  position: relative;
  background: #fff;
  border: 1px solid #ddd;
  transition: all 200ms ease-in-out;
}
.search-box.hovered, .search-box:hover, .search-box:active {
  border: 1px solid #aaa;
}
.search-box input[type=text] {
  border: none;
  box-shadow: none;
  display: inline-block;
  padding: 0;
  background: transparent;
}

.search-box input[type=text]:hover, .search-box input[type=text]:focus, .search-box input[type=text]:active {
  box-shadow: none;
}

.search-box .search-btn {
  position: absolute;
  right: 2px;
  top: 0px;
  color: #aaa;
  border-radius: 3px;
  font-size: 21px;
  padding: 5px 10px 1px;
  transition: all 200ms ease-in-out;
  display: block;
}

.search-box .search-btn:hover {
  color: #000;
}

.random-btn {
  color: #000;
  font-family: 'Roboto Condensed', sans-serif;
  font-size: 150%;
  margin: 2em;
}

.random-btn:hover {
  color: #9A12B3;
}

.random-icon {
  vertical-align: middle;
  padding-right: 1em;
}

#search-results {
  padding: 3em;
  margin: 3em;
  display: none;
  text-align: left;
}

@media screen and (min-width: 800px) {
  #search-results {
    width: 50%;
    position: absolute;
    margin: auto;
    top: 0; left: 0; bottom: 0; right: 0; /* Set the bounds in which to center it, relative to its parent/container */
  }
}

#search-results-heading {
  text-align: center;
}

#search-results-toolbar {
  display: none;
  padding-bottom: 3em;
  vertical-align: top;
  line-height: 26px;
}

#search-label {
  font-size: 120%;
  display: inline-block;
}

#new-search-btn {
  font-size: 120%;
  display: inline-block;
}

.search-result {
  font-size: 110%;
}
            
          
!
            
              $("#search-form").submit(function(){
    console.log("Submitted!");
    var searchTerms = $("#search-input").val();
    $("#search-menu").hide();
    $("#search-results").show();
    $("#search-results").css('display', 'inline-block');
    retrieveArticles(searchTerms);
    return false; // returning false to prevent page from reloading on form submission.
});

$('#search-btn').on('click',function(){
     console.log("Clicked!");
     $("#search-form").submit();
 });

$('#new-search-btn').on('click',function(){
  $(".search-result").remove();
  $()
  $("#search-results-placeholder").show();
  $("#search-label").empty();
  $("#search-results").hide();
  $("#search-results-toolbar").hide();
  $("#search-input").val('');
  
  $("#search-menu").show();
 });

function retrieveArticles(searchTerms) {
  console.log("Firing!");
  var wikiRequestUrl = "https://crossorigin.me/https://en.wikipedia.org/w/api.php?action=opensearch&search=" + searchTerms + "&format=json";
  
  $.ajax({
    url: wikiRequestUrl,
    datatype: "jsonp",
    success: function displayArticles(response) {
      console.log("Request successful");
      $("#search-label").text('Search results: "' + searchTerms + '"');
      
      var articleTitle = response[1];
      var articleDescription = response [2];
      var articleUrl = response[3];
      
      if (articleTitle.length > 0){
        for (var i = 0; i < articleTitle.length; i++){
          // add HTML elements displaying a preview of each article;
          
          var articleContent = "<h3>" + articleTitle[i] + "</h3>";
          articleContent += "<p>" + articleDescription[i] + "</p>";
          articleContent += "<p><a href='" + articleUrl[i] + "' target='_blank'>View full article</a></p>";
          var $newArticle = $("<div class='search-result'>" + articleContent + "</div>");
          $("#search-results").append($newArticle);
        }
        $("#search-results-placeholder").hide();
        $("#search-results-toolbar").show();
      } else {
        $("#search-results-placeholder").text("No relevant articles found");
        $("#search-results-toolbar").show();
      }
      
    },
    error: function(){
      console.log("Ajax failure!");
      $("#search-results-placeholder").text("We're experiencing difficulties connecting to Wikipedia!");
    }
  });
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console