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

              
                <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<div id="app">
  <p>Prototype for a FAQ with a "live search" that hides content not related to your search term</p>
  <input type="text" placeholder="Search" v-model="searchInput">
  <div class="container">
    <div class="left">
      <div class="category" @click="toggleCategory(0)" :class="[categories[0] ? activeCategory : '']"><h3>Making a Purchase</h3></div>
      <div class="category" @click="toggleCategory(1)" :class="[categories[1] ? activeCategory : '']"><h3>Buying Online</h3></div>
      <div class="category" @click="toggleCategory(2)" :class="[categories[2] ? activeCategory : '']"><h3>Delivery/Pickup</h3></div>
    </div>
    <div class="right">
      <div class="question-container" v-show="categories[0]">
        <div class="qa-pair">
          <h3 class="question" @click="toggleAnswer(0)">What does "Buyer's Best" mean?</h3>
          <p class="answer" v-show="questions[0]">On select products, our buyers have used The Big's purchasing power to get you the best quality products for the best prices in Canada.</p>
        </div>
        <div class="qa-pair">
          <h3 class="question" @click="toggleAnswer(1)">What does "Special Order" mean?</h3>
          <p class="answer" v-show="questions[1]">Special Order products are items we don't stock in our warehouses as regular inventory.</p>
        </div>
        <div class="qa-pair">
          <h3 class="question" @click="toggleAnswer(2)">What is the status of my order?</h3>
          <p class="answer" v-show="questions[2]">Your order may be in one of several statuses.</p>
        </div>
      </div>
      <div class="question-container" v-show="categories[1]">
        <div class="qa-pair">
          <h3 class="question" @click="toggleAnswer(0)">How do I know what items are in stock?</h3>
          <p class="answer" v-show="questions[0]">On each product details page, you can see what's in stock by looking at the delivery and pick-up dates listed underneath the item name and price.</p>
        </div>
        <div class="qa-pair">
          <h3 class="question" @click="toggleAnswer(1)">What payment methods can I use online?</h3>
          <p class="answer" v-show="questions[1]">You can pay online using your Big Visa Des Card, AMEX, MasterCard or Visa.</p>
        </div>
        <div class="qa-pair">
          <h3 class="question" @click="toggleAnswer(2)">Can I use my gift card for online purchases?</h3>
          <p class="answer" v-show="questions[2]">Unfortunately, gift cards for The Big are not able to be redeemed or purchased online at this time. You can purchase Big gift cards at any Big Group store and use them in store for any furniture, electronic, appliance or mattress purchase(s).</p>
        </div>
      </div>
      <div class="question-container" v-show="categories[2]">
        <div class="qa-pair">
          <h3 class="question" @click="toggleAnswer(0)">Where does The Big deliver?</h3>
          <p class="answer" v-show="questions[0]">Our Premium Delivery Service is available in most areas.</p>
        </div>
        <div class="qa-pair">
          <h3 class="question" @click="toggleAnswer(1)">Is Premium Delivery Service available in rural areas?</h3>
          <p class="answer" v-show="questions[1]">Because of factors like time constraints and extended travel times, we can offer only our Basic Service in some smaller towns and rural areas.</p>
        </div>
        <div class="qa-pair">
          <h3 class="question" @click="toggleAnswer(2)">Can I track my delivery?</h3>
          <p class="answer" v-show="questions[2]">In most major markets, you can track your delivery status on the day of your delivery using Find My Delivery.</p>
        </div>
      </div>
    </div>
  </div>
</div>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css?family=Lato&display=swap');

* {
  font-family: "Lato", sans-serif;
  margin: 0 auto;
  padding: 0;
  box-sizing: border-box;
  overflow: visible;
}

#app {
  padding: 1em;
  height: 1000px;
  input {
    margin-bottom: 1em;
  }
}

.container {
  display: flex;
  flex-wrap: wrap;
}

.left {
  width: 35%;
  .category {
    padding: 1em;
    border-bottom: 1px solid lightgray;
    cursor: pointer;
    &:hover {
      background: #eee;
    }
  }
  .activeCategory {
    background: #e9292c;
    h3 {
      color: #fff;
    }
    &:hover {
      background: #e9292c;
    }
  }
}

mark {
  background: pink;
  padding: 1px 0;
}

.right {
  width: 65%;
  padding-left: 1em;
  padding-right: 1em;
  .qa-pair {
    margin-bottom: 1em;
    h3 {
      margin-bottom: .5em;
      cursor: pointer;
      position: relative;
      padding-bottom: .5em;
      &::before {
        content: '';
        position: absolute;
        height: 1px;
        width: 100%;
        background: black;
        bottom: 0;
      }
      &:hover::before {
        height: 3px;
      }
    }
  }
}
              
            
!

JS

              
                var app = new Vue({
  el: '#app',
  data: {
    //values here represent the active category or active questions
    categories: [
      true, false, false, false, false, false, false
    ],
    questions: [
      false, false, false, false, false
    ],
    searchInput: null,
    activeCategory: 'activeCategory'
  },
  methods: {
    toggleCategory: function(category) {
      this.searchInput = ''
      if (this.categories[category] == false) {
        this.hideOtherCategories();
        this.$set(this.categories, category, true);
        this.hideAnswers();
      }
    },
    toggleAnswer: function(question) {
      if (this.questions[question] == false) {
        this.$set(this.questions, question, true);
      } else {
        this.$set(this.questions, question, false);
      }
    },
    hideAnswers: function() {
      for (i=0;i<this.questions.length;i++) {
        this.$set(this.questions, i, false);
      }
    },
    hideOtherCategories: function() {
      for (i=0;i<this.categories.length;i++) {
        this.$set(this.categories, i, false);
      }
    },
    showAllAnswers: function() {
      for (i=0;i<this.questions.length;i++) {
        this.$set(this.questions, i, true);
      }
    },
    showAllTopics: function() {
      for (i=0;i<this.categories.length;i++) {
        this.$set(this.categories, i, true);
      }
    },
    search: function(category) {
      var input = this.searchInput.toUpperCase();
      input = input.normalize("NFD").replace(/[\u0300-\u036f]/g, "") //does not work with IE 11
      var allQuestions = this.$el.querySelectorAll('.question');
      var allAnswers = this.$el.querySelectorAll('.answer');
      var qaPairs = this.$el.querySelectorAll('.qa-pair');
      this.showAllAnswers();
      this.showAllTopics();
      this.activeCategory = ''
      
      //Mark.js instance which allows search words to be highlighted
      var container = document.querySelector('.right')
      var instance = new Mark(container)
      instance.unmark();
      instance.mark(input, { "separateWordSearch": false }) //allows words with spaces to be found
      
      if (input) {
        for (i = 0; i < qaPairs.length; i++) {
          var answers = allAnswers[i].innerHTML.toUpperCase().indexOf(input)
          var questions = allQuestions[i].innerHTML.toUpperCase().indexOf(input)
          
          if (answers == -1 && questions == -1) {
            qaPairs[i].style.display = 'none';
          } else {
            qaPairs[i].style.display = 'block';
          }
        }
      } else {
        for (i = 0; i < qaPairs.length; i++) {
          qaPairs[i].style.display = 'block';
        }
        this.hideAnswers();
        this.hideOtherCategories();
        this.$set(this.categories, 0, true);
        this.activeCategory = 'activeCategory'
      }
    }
  },
  watch: {
    searchInput: function() {
      this.search();
    }
  }
})
              
            
!
999px

Console