Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a good way to validate jQuery code?

Tags:

jquery

I am a bonifide jQuery novice and I have some jquery code that I have pieced together, but I keep getting an error when the page loads. Problem is I am not sure if I have all the curly brackets and parenthesis closed properly and would really appreciate another set of eyes on this:

$(document).ready(function() {
    $('#slideshow').cycle({
        fx:  'scrollLeft', 
        timeout: 8000
    });
});
    $('#freeQuote form')
       .validate({
         submitHandler: function(form) {
           $(form).ajaxSubmit({
                success: function() {
                var Image = $('<img />').attr({src:'_images/free-quote-confirm.png', width:231, height:267, alt:"Success"});
                    $('#freeQuote form').hide();
                    $('#freeQuote').append(Image)
                }
           });
         }
       }); 

  $('#news-signup form')
    .validate({
     submitHandler: function(form) {
       $(form).ajaxSubmit({
            success: function() {
            var Image = $('<img />').attr({src:'_images/register-thanks.png', width:332, height:35, alt:"Success"});
                $('#news-signup form').hide();
                $('#news-signup').append(Image)
            }
       });
     }
  }); 

I have this loading from its own .js file.

Thanks.

like image 325
forrest Avatar asked Oct 01 '10 20:10

forrest


Video Answer


2 Answers

Yes, load the page in Chrome and press Ctrl-Shift-J to bring up the JavaScript console. This will instantly highlight any syntax errors.

Alternatively, get Firebug for Firefox.

As for your code, I think you should start smaller if you don't know what you're doing. Get the cycle() function working first, then try one of the validations, then the other. Make one bit work at a time, and every time you add something new, try it out to see if it works, or doesn't, and if it generates errors.

Once you get to that stage, follow Robert's advice and run it through JSLint. That'll show you where you're doing things wrong, like missing semicolons on the ends of statements.

like image 188
Skilldrick Avatar answered Nov 15 '22 21:11

Skilldrick


Yes, http://www.jslint.com/

Your code returned the following errors:

Error:

Problem at line 14 character 50: Missing semicolon.

$('#freeQuote').append(Image)

Problem at line 27 character 48: Missing semicolon.

$('#news-signup').append(Image)

Implied global: $ 1,2,7,10,12,13,14,20,23,25,26,27, document 1

JSLint hates everything I do :P

like image 30
Robert Greiner Avatar answered Nov 15 '22 21:11

Robert Greiner