Skip to content

Instantly share code, notes, and snippets.

@jongrover
Last active December 19, 2015 01:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jongrover/5878908 to your computer and use it in GitHub Desktop.
Save jongrover/5878908 to your computer and use it in GitHub Desktop.
Student Sinata App: JS - Search Box lab instructions

Student Sinata App: JS - Search Box

Objective

Add JavaScript Behavior to your existing Sinatra App. You will be adding the functionality to allow users to type search term into an input field and click a button to search the students page and highlight content.

Steps

  1. First use git to create a new feature branch called search-box. Checkout this branch and use it to build the serach box feature.

  2. In your index page that lists all students create a text input and button to allow users to search the list of students using specific terms.

 <!-- SEARCH -->
<div id="search">
  <label>search: </label>
  <input id="" type="text">
  <button id="">find</button>
</div><!-- #search -->
  1. Set the input field and button id attributes with a semantic name that you can use to select them with later on using jQuery.

  2. Create an 'app.js' file in your ./public/js/app.js folder and make sure to link to it from your index.erb file.

  3. Prepare the app.js file using document ready statement you have learned.

  4. Use jQuery to create an event listener click() that runs whenever the search button is clicked.

  5. The search function mentioned above should get the input from the search_input field.

  6. After gathering the users search term, insert it into a jQuery selector statement to look for all students that match the search term. Hint: Make use of jQuery's :contains() and :not() selectors.

  7. For all the matching terms in the search apply a jQuery effect or css change of your choice. For example you could highlight the search term or the students div using CSS or yu could apply an animation or fade effect to light up the selected student. Have fun!

  8. Bonus: If you complete all of the above try making an alternate solution using jQuery's keyup() to get the search to trigger and update as they type in the search input field. Try to get the serach to be case-insensitive, so both uppercase and lowercase will return the correct matches. Get the form to trigger on the Return key. See link

Useful Lab Resources:

JavaScript Reference - Mozilla Developers Network

jQuery API

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment