Random Quotes in Jquery

In building Groupsinteractive.com, we tried to incorporate several elements to make the site more, well, interactive. One of those features is the randomly pulled quotes about our product that appear in the bottom right hand corner. Each time the page is refreshed, a new quote appears. We are obviously not the first company to incorporate this technique. I have been reading an advertising blog for several years now which has this feature, and I love it. I though I would take a few minutes and explain how we got it working on our site.

My user interface magic tool of choice is Jquery, which is a javascript library built by John Resig. I love it because it simplifies some of the more tedious elements of javascript, and allows me to do more coding in less time.

Step 1 – The Markup

I built my html and css before I knew I would do the randomly appearing quotes in Jquery, so this may not be the most efficient markup, but this is how I did it.

In the footer, I put the following html:

<div id="quote-box-text">
    <div>This is my first quote</div>
    <div>This is my second quote</div>
    <div>This is my third quote</div>
</div>

  <div id="quote-author">
    <div>
      <h3>Author1</h3>
      <h4>AuthorPlace1</h4>
    </div>

    <div>
      <h3>Author2</h3>
      <h4>AuthorPlace2</h4>
    </div>

    <div>
      <h3>Author3</h3>
      <h4>AuthorPlace3</h4>
    </div>
  </div>

  <div id="quote-box">
    <img src="images/quote-box.png">
  </div>

</div>

You will notice that there are three elements- the quote text, the author and where they are from, and the quote box itself (which is an absolutely positioned png image). These three elements all sit inside a div with a class of “grid_3.” (I am using the 960 grid system developed by Nathan Smith.)

Nothing too fancy here.

Step 2 – The CSS

Next, we look at the styling. Now, you can style the elements anyway you like, what I am going to show you is the styling necessary to make the quotes randomly appear.

css:

#quote-box-text div, #quote-author div {
 display: none;
}

Simple, right? All I do is tell the browser not to display any div element contained in an element with an id of #quote-box-text or #quote-author.

At this point, if someone were to load the page, nothing would appear in the quote box because the content is hidden. When we add the Jquery, that’s when the magic happens.

Step 3 – The Jquery

The first step here is, obviously, to include the Jquery library in your header. Fortunately, google hosts Jquery, so you don’t even have to download if you don’t want. You can link to it directly at:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/Jquery/1.3.2/Jquery.min.js"></script>

Once you have placed that in your header, open a script tag and input the following:

<script type="text/javascript" charset="utf-8">
$(function() {
 var randomNum = Math.floor(Math.random()*3);
$('div#quote-box-text div:eq(' + randomNum + ')').css("display", "block");
$('div#quote-author div:eq(' + randomNum + ')').css("display", "block");
});
</script>

Let me explain what is happening here.

First, we use 2 javascript functions, math.floor and math.random, to generate a random number. We use the combination so that the randomly generated number does not lean too far to either side (up or down). Thanks to http://www.javascriptkit.com/javatutors/randomnum.shtml for that explanation

The number after the Math.random function MUST match the number of quotes you have, or else one of two things will happen:

Not all of your quotes will be “put in the hat” to be randomly pulled or it will try to access a div that does not exist and nothing will show up.

Secondly, we use some Jquery to display a div based on the random number we just created. The Jquery function :eq() will find the nth child of a given element, in this case the nth (where n is the random number we generated) div of the div with an id of #quote-box-text. You can read more about the eq() function here.

We then do the same thing to display the quote author, using the same randomly generated number, and we are done.

Notice that the only thing Jquery is really doing for us is changing the css to display:block instead of display:none. CSS and HTML do the rest.

See a better way this could be accomplished? Please comment and let me know.

Tags: ,

Leave a Reply

Groups Interactive small group software

Small group ministry software solution for your church and ministry

Safe, secure, and easy way to manage your small group ministry.

  • Easily integrates with your church website in minutes.
  • Built in mapping and filtering make it easy for people to find the best group for them.
  • Group leaders receive automatic e-mail reminders to post group attendance and notes each week.
  • Administrators and leaders can quickly generate attendance, absentee, and other reports.
  • Groups can connect through chat, online studies, photos, group calendars and more.
  • Don't need another place to login? Groups Interactive is designed to push information out to where your group members are. They can even subscribe to their group through their favorite calendar software.
  • Groups Interactive small group software allows your small group leaders to post their attendance and group notes from their home or workplace over the Internet. They can even use it to communicate to their whole group. This weekly touch point helps even infrequent attenders feel connected and helps coaches, directors and pastors know the condition of their groups better than ever before. It works as well with Sunday schools as off-campus small group ministries. In fact, if you are using a blended model, it will help you manage both.

    While managing small groups can be time consuming, Groups Interactive creates a process that is simple for you and your group leaders. Once you’ve tried Groups Interactive small group software, we think you’ll agree.