views:

49

answers:

3

I recently transferred a site to a new host. Reloaded everything, and the javascript that worked fine before is breaking at an element it can't find. $('#emailForm') is not defined.

Now, the #emailform isn't on the page, but it wasn't before either, and JS used to skip this and it would just work. Not sure why this is happening. Any clues

Here is the site I am having the prblem:

http://rosecomm.com/26/gearwrench/

A: 

I'm not sure why it would be skipped before, but to avoid the error, wrap the statement(s) that reference $('#emailForm') in an if statement that checks to see if it is present:

if ( $('#emailForm').length ) {
    // code to handle $('#emailForm') goes here...
}
David Lantner
If you are calling jQuery functions (like binding events, etc), you don't need to check the length. If there are no elements found `jQuery()`/`$()` will return an empty jQuery object. The jQuery functions are all safe to call on an empty jQuery object.
gnarf
+2  A: 

jQuery will return an empty jQuery object from $('#emailForm') if there isn't an element with the id='emailForm'.

One of the following is likely true:

  • You forgot to include jQuery - therefore $ is undefined.
  • There is another library included that uses $ - in which case you can wrap your code in a quick closure to rename jQuery to $

The Closure:

(function($){ 
   // $ is jQuery
   $('#emailForm').whatever();
})(jQuery);

You could console.log(window.$,window.jQuery); in firebug to check for both of these problems.

gnarf
+1 - jQuery not being uploaded or with the connect permissions on the new host is the most likely culprit.
Nick Craver
it's definitely uploaded. I think it's some weird directory issue. Because I had it working at some point in a subfolder then moved it.i have an .htaccess file with RewriteEngine OnRewriteBase /RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule . /index.php [L]And then the jquery is linking to http://mysite.com/js/jquery.jsI echoed this out in php to make sure it was working, and then put that url in the browser and the js is there. Does this mod-rewrite look ok, or is it the culprit?
pfunc
Firebug will likely solve your problem, make sure that the scripts are properly included, etc...
gnarf
Firebug is giving me the famous $ is not a function. But the included script tags point in the right direction. I take all the other JS scripts off the page, except jquery, and it still happens, So it's not a conflict.
pfunc
Is `jQuery` a function? If not- jQuery isn't properly included. If so - use the closure, you may be using `noConflict()` mode... Try pulling up the `jQuery.js` request in the net panel, make sure its serving jQuery and not a 404 error, etc...
gnarf
I called the jquery and it comes up fine. I wrap it in the closure, and same problem.
pfunc
+2  A: 

You have mootools-1.2.2-core-yc.js installed as well, and it is conflicting with jQuery.

http://docs.jquery.com/Using_jQuery_with_Other_Libraries

$(document).ready(function() {

(function($){

 // bind 'myForm' and provide a simple callback function
 $('#emailForm').ajaxForm(function() {
     var txt=document.getElementById("formReturn")
     txt.innerHTML="<p>Thank You</p>";
 });
... 

$(document).ready is being called against the moo tools library instead of jQuery.

patrick dw
wrap the whole `$(document).ready()` call in the `(function($) { ... })(jQuery)` closure, it should work fine after that, and any calls to `$` inside that closure will be bound to the correct library
gnarf