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

            
              <div class="container" id="container-div">
      <h1>Wikipedia Viewer</h1>
      <div class="row">
            <div class="col s3"></div>
            <form class="input-field col s6" method="POST" id="search-form">
                  <input id="search-box" placeholder="Search me" type="text">
                  <input type="submit" class="hidden" />
            </form>
            <div class="col s3"></div>
      </div>
      <div class='row'>
            <div class="chip" id="search-term"></div>
      </div>
            <a href="https://en.wikipedia.org/wiki/Special:Random" target="_blank" id="random-search-btn" class="waves-effect waves-light btn">Random</a>

      <div id="results-container"></div>

</div>
            
          
!

CSS

            
              .hidden {
      display: none;
}
.container {
      text-align: center;
}

#random-search-btn {
      margin-bottom: 20px;
}
            
          
!

JS

            
              // API url string to add
var apiUrl = "https://crossorigin.me/http://en.wikipedia.org/w/api.php?action=opensearch&search=";
var finalUrl = "";
// var returnJson = {}; Do we need to decalre the global obj
var searchInput = "";

var searchArr = [];

// Add event listener to capture form input
var form = document.getElementById("search-form");
if (form.attachEvent) {
      form.attachEvent("submit", captureForm);
} else {
      form.addEventListener("submit", captureForm)
}

// var z = document.getElementById('random-search-btn');  TO DELETE IF BUTTON WORKS WITHOUT
// z.addEventListener('click', randomSearch);

// function to reset the variables used in the search() function 
function resetVariables() {
      finalUrl = undefined;
      searchInput = undefined;
      searchArr = undefined;
}


// deleteResults() fucntion to remove DOM elements from last search
function deleteResults() {
      var myNode = document.getElementById('results-container');
      while (myNode.firstChild) {
            myNode.removeChild(myNode.firstChild);
      }
}

function captureForm(e) {
      resetVariables();
      deleteResults();
      // returnJson = {}; is this the best way to clear the obj each new search
      if (e.preventDefault) e.preventDefault();
      // log form input into searchInput variable
      searchInput = document.getElementById("search-box").value;
      // Have to return false to prevent default behaviour
      if (searchInput !== "") {
            finalUrl = apiUrl + searchInput + "&format=json";
            getSearchData();
      } else {
            console.log("user did not search anything");
            // MUST ADD -- UI interaction to let the user know they need to search something
      }

}

// Create the XHR object.
function getSearchData() {
      var request = new XMLHttpRequest();
      request.open('GET', finalUrl, true);
      request.onload = function() {
            console.log("function has fired");
            if (request.status >= 200 && request.status < 400) {
                  // Success!
                  searchArr = JSON.parse(request.responseText);
                  console.log(searchArr);
                  updatePage();
            } else {
                  // We reached our target server, but it returned an error
            }
      };
      request.onerror = function() {
            // There was a connection error of some sort
      };
      request.send();
}

function updatePage() {
      // Add search term to Materialze chip compontent
      document.getElementById("search-term").innerHTML = searchArr[0];
      // code loops through the array from the JSON API response to dynamically build the HTML elements containing the search results and adds them to page
      for (var i = 0; i < searchArr[1].length; i++) {
            var div = document.createElement('div');
            div.className += 'col s10';
            var a = document.createElement('a');
            a.href = searchArr[3][i];
            a.setAttribute("target", "_blank");
            var innerDiv = document.createElement('div');
            innerDiv.className += 'card-panel teal white-text';
            var contentTitle = document.createElement('h5');
            contentTitle.innerHTML = searchArr[1][i];
            var content = document.createElement('p');
            content.innerHTML = searchArr[2][i];
            contentTitle.appendChild(content);
            innerDiv.appendChild(contentTitle);
            a.appendChild(innerDiv);
            div.appendChild(a);
            // completed Div to page
            document.getElementById('results-container').appendChild(div);
      }
}

            
          
!
999px

Console