Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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

              
                  <!-- Modal -->
    <div class="modal-container">
      <button type="button" name="button" class="modal-btn">
        <i class="material-icons md-14">info_outline</i></button>
      <dialog class="modal-about">
        <button type="button" name="button" class="modal-cancel">
          <i class="material-icons sm-12">clear</i></button>
        <h4 class="modal-title">Wikipedia App</h4>
        <p class="modal-text copy">This is an Intermediate Front End Developer project
          from the FreeCodeCamp curriculum. The user stories to be completed are:</p>
        <ul class="user-stories">
          <li class="story copy"> I can search Wikipedia entries in a search box and see the resulting Wikipedia entries.</li>
          <li class="story copy">I can click a button to see a random Wikipedia entry.</li>
        </ul>
        <p class="modal-text copy">This was made using: Flexbox, SCSS, and native JS.
          No CSS or JavaScript frameworks or libraries were used.
          The code can be seen on
          <a href="https://github.com/D-Pagey/Wikipedia" target="_blank" class="white-link">Github</a>.</p>
      </dialog>
    </div>

<div class="main-container" id="container">

      <h1 class="title">Wikipedia Viewer</h1>

      <div class="search-section">
        <button class="button back-button hide" id="back">Back</button>
        <input type="text" class="input" placeholder=" Search for an article..." value="" id="userSearch">
        <button class="button search-button" id="submit">Search</button>
      </div>

      <a href="https://en.wikipedia.org/wiki/Special:Random" target="_blank"><button type="button" class="random-button" id="random">Random Article</button></a>

      <!-- Search Results -->
      <div id="search-results">
      </div>

      <!-- Footer -->
      <div class="footer" id="foot">
        <p>Written and coded by <a href="https://www.freecodecamp.org/d-pagey" target="_blank" class="freecodecamp link">Pagey</a></p>
      </div>

    </div>
              
            
!

CSS

              
                /* Variables */
$article-color: darkgray;
$tablet: "(min-width: 750px)";

/* Mixins */
@mixin mediaQueries($arg) {
  @media screen and #{$arg} {
    @content;
  }
}

* {
  background-color: #f0ead6;
  font-family: 'Montserrat', sans-serif;
  font-size: 14px;
  margin: 0;
  padding: 0;
  @include mediaQueries($tablet) {
    font-size: 16px;
  }
}

.main-container {
  align-items: center;
  display: flex;
  flex-direction: column;
  margin: 0 auto;
  max-width: 500px;
  width: 95%;
}

/* Modal */
.modal-container {
  display: inline-flex;
  justify-content: flex-end;
  width: 100%;
}

.modal-btn {
  background-color: transparent;
  border: none;
  margin: 5px 5px 0 0;
  opacity: .85;
}

.sm-12 {
  background-color: white;
}

.material-icons.md-14 {
  font-size: 20px;
}

.modal-about {
  background-color: white;
  border: 2px solid black;
  border-radius: 5px;
  margin: 0 auto;
  padding: .5em;
  top: 100px;
  width: 80%;
  max-width: 400px;
}

.modal-cancel {
  background-color: white;
  border: none;
  float: right;
}

.material-icons.sm-12 {
  font-size: 20px;
}

.modal-title {
  background-color: white;
  color: royalblue;
  font-size: 1.2em;
  padding: .5em;
  text-decoration: underline;
}

.user-stories {
  background-color: white;
  font-style: italic;
  padding: .5em 2em .5em 2.5em;
}

.story:nth-of-type(2) {
  margin-top: 1em;
}

.modal-text {
  background-color: white;
  margin: .5em;
}

.copy {
  background-color: white;
  font-size: 1.1em;
}

.white-link {
  background-color: white;
  color: royalblue;
  font-weight: 600;
  text-decoration: none;
}

.modal-about::backdrop {
  background: darkgray;
  opacity: .85;
}

/* Main Content */
.title {
  font-size: 2.2em;
  font-weight: 300;
  margin: 1em 0 .5em 0;
}

.search-section {
  display: flex;
  justify-content: center;
  margin: 1em 0;
  width: 90%;
}

.input {
  background-color: white;
  display: inline-block;
  padding: 1px 0;
}

.button {
  border: .5px solid;
  border-radius: 2px;
}

.hide {
  display: none;
}

.show {
  display: initial;
}

.search-button {
  margin-left: 5px;
}

.back-button {
  align-self: flex-start;
}

.search-button, .back-button {
  background-color: lightgrey;
  font-weight: bold;
  padding: 5px 6px;
}

.random-button {
  background-color: #007bff;
  border: 1px solid transparent;
  border-color: #007bff;
  border-radius: .25em;
  color: #fff;
  display: inline-block;
  line-height: 1.25;
  padding: .5em .75em;
}

.link {
  color: royalblue;
  font-weight: 600;
  text-decoration: none;
}

.footer {
  bottom: 0;
  height: 30px;
  left: 0;
  margin-bottom: 10px;
  position: fixed;
  text-align: center;
  width: 100%;
}

.footer--not-fixed {
  bottom: 0;
  height: 30px;
  left: 0;
  margin-bottom: 10px;
  position: initial;
  text-align: center;
  width: 100%;
}

.freecodecamp {
  color: #007bff;
  text-decoration: none;
}

/* Search Results */
.articles {
  background-color: white;
  border: 2px solid;
  border-radius: .5em;
  font-size: .8em;
  margin: 0 auto 1em;
  width: 97.5%;
}

.articles:hover,
.articles:hover .article-headers,
.articles:hover .article-snippets {
  background-color: $article-color;
}

.article-headers {
  background-color: white;
  font-size: 1.5em;
  margin: .5em;
}

.article-link {
  text-decoration: none;
}

.article-snippets {
  background-color: white;
  color: black;
  margin: .5em;
  width: 97.5%;
}

              
            
!

JS

              
                // Constants
const userSearch = document.getElementById("userSearch");
const searchBtn = document.getElementById("submit");
const content = document.getElementById("search-results");
const backBtn = document.getElementById("back");
const random = document.getElementById("random");
const footer = document.getElementById("foot");
const apiUrl = "https://en.wikipedia.org/w/api.php?action=query&format=json&origin=*&list=search&srsearch=";

function createHeader(titleV) {
    const newHeader = document.createElement("h3");
    newHeader.className = "article-headers";
    const title = document.createTextNode(titleV);
    newHeader.appendChild(title);
    return newHeader;
}

// Creating new Div
function addDiv(titleV, snippetV, linkV) {
  const newDiv = document.createElement("div");
  newDiv.className = "articles";

  const newHeader = createHeader(titleV);

  const newLink = document.createElement("a");
  newLink.href = linkV;
  newLink.target = "_blank";
  newLink.className = "article-link";

  const newSnippet = document.createElement("p");
  newSnippet.className = "article-snippets";
  const snippet = document.createTextNode(snippetV);
  newSnippet.appendChild(snippet);
  newLink.appendChild(newSnippet);

  newDiv.appendChild(newHeader);
  newDiv.appendChild(newLink);
  content.appendChild(newDiv);
}

function removeDivs() {
  const divs = document.getElementsByClassName("articles");

  while (divs.length > 0) {
      content.removeChild(divs[0]);
  }
}

// Toggle hiding/showing buttons
function hide(element) {
  if (element == footer) {
    element.className = "footer--not-fixed";
  } else if (element == backBtn) {
    element.className = "button back-button show";
  } else {
    element.className = "hide";
  }
}

function show() {
  backBtn.className = "button back-button hide";
  searchBtn.className = "button search-button show";
  random.className = "random-button show";
  userSearch.className = "input show";
  footer.className = "footer";

  removeDivs();

  userSearch.placeholder = " Search for an article...";
  userSearch.value = "";

}

// if no content
function contentCheck() {
  (userSearch.value === '' ? alert("You need to search for something!"): apiCall());
}

function extractJson(data) {
  return data.json();
}

// apiCall
function apiCall() {
  fetch(apiUrl + userSearch.value)
    .then(extractJson)
    .then(showResults)

    .catch(function(error) {
      console.log("Something went wrong " + error);
    })
  }

// Display search results
function showResults(data) {
  const { query: { search } } = data;

  hide(backBtn);
  hide(searchBtn);
  hide(random);
  hide(userSearch);
  hide(footer);

  removeDivs();

  for (let i = 0; i < search.length; i++) {

   addDiv(search[i].title, search[i].snippet.replace(/<\/?[^>]+>/gi, ''), `https://en.wikipedia.org/?curid=${search[i].pageid}`);

  }
}

// Event Listeners
searchBtn.addEventListener("click", contentCheck);
backBtn.addEventListener("click", show);

/* To Do:
- map instead of if statement
*/

// Modal Functionality
const modalBtn = document.getElementsByClassName('modal-btn');
const modal = document.getElementsByClassName('modal-about');
const button = document.getElementsByClassName('modal-cancel');

modalBtn[0].addEventListener('click', function() {
  modal[0].showModal();
});

button[0].addEventListener('click', function() {
  modal[0].close();
})

              
            
!
999px

Console