Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

jQuery fortune teller not very talkative.. could use some help.

After taking the jQuery lessons, I have attempted to write my first program - a simple fortune teller program. You ask a question (right now for love, happiness or money) and the program takes the question, splits it into an array and then loops through the words for a keyword. Depending on the keyword, one of ten random answers is returned. The problem I am having is that sometimes it gives the answer but only for a split second and then resets itself, or sometimes the h1 text where the answers are returned, suddenly is empty. Any ideas, comments, suggestions, or polite critiques are welcome and appreciated. Thank you!

Here is the link to the program in action: http://laurelnatale.me/fortune/ You can currently ask it questions with love, money, wealth, happy, happiness.

Here is the code: http://laurelnatale.me/fortune/fortune.js

1 Answer

My tenderfoot answer is the page refreshes after the "ask" button is clicked which of course erases the HTML injection performed by the JQuery code.

fortune.js has "return false" to stop the page refresh but its targeting the wrong object. To target the proper object, try this:

$("form").submit(function(e) { return false; ....

Here a quick discussion on stackoverflow on the topic: http://stackoverflow.com/questions/6462143/prevent-default-on-form-submit-jquery

Good luck.

Thank you jesse!!!. I read the discussion on the link you provided and a simple e.preventDefault(); did the trick.