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

Help with a personal jQuery problem :P

So I'm working on CodePen trying to make something cool which is a drop down 3d box to submit emails. The problem is I'm using jQuery to remove the "complete" class when the reset button is hit but it just goes straight back to loading?

Here's the link -> http://codepen.io/lesniall/pen/bVNqex

Any help would be appreciated

4 Answers

Hi, I have solved the problem but it brings another. I believe that your submit is too general and that when you press reset it's also calling the resetButton function. I have a basic solution below and it works but for some reason wont add the loading class, probably an easy fix. hope it helps.

  //on form submit remove .ready and add .loading to the form
  var $resetButton =  $("#cuboid form .submit-button")
  $resetButton.click(function() {
    $resetButton.removeClass("ready").addClass("loading");
    //finish loading in 3s
    setTimeout(complete, 3000);
    //prevent default form submission
    return false;
  })

  function complete() {
      $("#cuboid form").removeClass("loading").addClass("complete");
    }
    //reset/refresh functionality
  $(".reset-button").click(function() {
    $("#cuboid form").removeClass("complete").addClass("nothing");
  })

Your reset button is inside your form element. When you click 'reset' you're calling the form submit event again. Move your reset button markup outside the form and adjust styles accordingly. This fixes the issue. If you do want to keep the reset button inside the form (best practices and whatnot), you'll need to add a conditional to check the value of the text on the button on click (or a class name, etc...)

Here's one more option-the essence of the issue is both of the button elements inside the form are calling the submit event, therefore causing you to loop through the loading/complete cycle.

  //on click of the submit button do your fancy CSS 
  $('#cuboid .submit-button').click(function(){
    $('#cuboid form').removeClass("ready").addClass("loading");
    //finish loading in 3s
    setTimeout(complete, 3000);
  })


  $("#cuboid form").submit(function(event) {
  $('#cuboid input').val("");
   event.preventDefault();
    //clear out the form andprevent default form submission-you'll need to submit with JavaScript
  })

Thanks for the reply I can see this taking a while I might just need to remove the reset button for now it's boggling me :P