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" 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>
            
          
!
            
              .hidden {
      display: none;
}
.container {
      text-align: center;
}

#random-search-btn {
      margin-bottom: 20px;
}
            
          
!
            
              // 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
🕑 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