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-fluid col-sm-8 col-sm-offset-2">
  
  <h1>Wikipedia Search</h1>
  
  <form class="wiki-search">
    <div class="input-group">
      <input id="search-input" type="text" class="form-control" aria-label="Search Wikipedia for..."/>
      <span id="clear-search" class="input-group-addon"><i class="fa fa-times" aria-hidden="true"></i></span>
      <span id="search-btn" class="btn btn-primary input-group-addon"><i class="fa fa-search" aria-hidden="true"></i></span>
    </div>
  </form>

  <a id="random-page" href="https://en.wikipedia.org/wiki/Special:Random" target="_blank">I'm Feeling Random</a>
  
</div>

<div id="results" class="container-fluid col-sm-8 col-sm-offset-2">

</div>
              
            
!

CSS

              
                body {
    background-color: rgb(249, 253, 255);
}

h1 {
  font-family: 'Open Sans Condensed', sans-serif;
}

#min-logo {
  border-radius: 0;
  font-size: 18px;
  padding-left: 8px;
  padding-right: 8px;
}

#search-input {
  border-radius: 0;
  border-right: 0;
  box-shadow: none;
}

input.form-control:focus {
  box-shadow: none;
  border-color: rgb(204, 204, 204);
}

.btn.btn-primary {
  border-radius: 0;
  padding: 2px 6px;
  font-size: 20px;
  background-color: rgb(51, 122, 183);
  border-color: rgb(46, 109, 164);
  color: #fff;
}

#clear-search {
  display: none;
  background-color: #fff;
  border-left: 0;
  font-size: 18px;
}

#clear-search:hover {
  cursor: pointer;
}

#random-page {
  width: 130px;
  margin: 10px auto;
  display: block;
}

#results {
  margin-top: 10px;
}

.tile {
  padding: 6px 12px;
  margin: 10px 0;
  background-color: #fff;
  box-shadow: 0 2px 5px 0 rgba(0,0,0,0.16),0 2px 10px 0 rgba(0,0,0,0.12);
}

.tile h3 {
  margin: 10px 0 0 0;
}

.tile p:nth-of-type(2) {
  border-top: 1px solid rgba(0,0,0,0.16);
  padding-top: 15px
}

.pager li>a,
.pager li>span {
  border-radius: 0;
}

/** Post Search Submission **/
.searching #clear-search {
  display: table-cell;
}

.searching #random-page {
  display: none;
}

.searching h1 {
  display: none;
}

.wiki-search {
  margin-top: 10px;
}

@media screen and (max-width: 768px) {
  
  .search-container {
    padding-top: 30px;
  }
  
  h1 {
    font-size: 20px;
    display: block;
    width: 100%;
    margin-top: 10px;
    margin-bottom: 10px;
    box-sizing: border-box;
  }
  
}
              
            
!

JS

              
                //How to store this pagination value without using a global variable?
var searchOffset = 0;

//Core Search Function
function wikiSearch() {

  var searchQuery = $('.wiki-search input').val();
  
  $.ajax({
    url: '//en.wikipedia.org/w/api.php',
    data: {
      format: 'json',
      action: 'query',
      list: 'search',
      srsearch: searchQuery,
      sroffset: searchOffset,
      continue: ''
    },
    dataType: 'jsonp',
    headers: { 'Api-User-Agent': 'FCC-J-Search-App/1.0' },
    success: function(data) {
      
      //Update UI Layout
      $('.container-fluid').addClass('searching');
      if ( !$('#min-logo').length ) {
        $('#search-input').before('<span class="input-group-addon" id="min-logo">WS</span>');
      }
      
      
      //Send results to DOM
      $('#results').html(
        '<div>Your query returned ' + 
        data.query.searchinfo.totalhits + 
        ' results </div>'
      );

      for(var i = 0; i < data.query.search.length; i++ ) {
        $('#results').append(
          '<div class="tile"><h3><a href=\"//en.wikipedia.org/wiki/' + data.query.search[i].title + "\" target=\"_blank\" >" + 
          data.query.search[i].title + 
          '</a></h3><p>' + 
          "<a href=\"//en.wikipedia.org/wiki/" + data.query.search[i].title + "\" target=\"_blank\" >en.wikipedia.org/wiki/" + 
          data.query.search[i].title + 
          "</a>" + 
          '</p><p>' + 
          data.query.search[i].snippet + 
          '...</p></div>'
        );
      }

      //Show pagination button, and increment the searchOffset
      if (data.query.searchinfo.totalhits > 10) {
        $('#results').append('<nav id="pagination"><ul class="pager"><li class="next"><a id="pagNext" href="#">Next <i class="fa fa-angle-right" aria-hidden="true"></i></a></li></ul></nav>');
        if (searchOffset > 0) {
          $('#pagination').removeClass('text-center');
          $('#pagination .pager').prepend('<li class="previous"><a id="pagPrev" href="#"><i class="fa fa-angle-left" aria-hidden="true"></i> Previous</a></li>');
        }
      }    
      
      //Pagniation Buttons
      $('#pagPrev,#pagNext').on('click', function() {
        $('html, body').animate({scrollTop: 0}, 500);
        if ( $(this).attr('id') === 'pagPrev') {
          searchOffset -=11;
          wikiSearch();
        } else {
          searchOffset += 11;
          wikiSearch();
        }
      });
      
    },
    error: function(data) {
      $("#results").html('<p>Request Failed.</p>');
      $("#results").append('<p>' + JSON.stringify(data) + '</p>');
    }
});
      //Prevent Page Reload
      return false;
}

//Fire the search!
$("#search-btn").on('click', function() {
    $('.wiki-search').submit();
});
$('.wiki-search').on('submit', function(e) {
  e.preventDefault();
  if ( $('#search-input').val() !== '' ) {
    document.activeElement.blur(); //Needed for mobile device keyboard clearing
    wikiSearch();
  }
});

//Clear current search
$("#clear-search").on('click', function() {
  searchOffset = 0;
  $("#search-input").val('').focus();
});
              
            
!
999px

Console