views:

611

answers:

13

Mine is that, I almost always forget to put a # before the id name of the element

Example:

$("ElementId") instead of $("#ElementId")

Whats yours?

+18  A: 

Not really a jQuery issue as much as a JavaScript issue: Leaving extra commas when using object notation after removing the last property:

var point = {
    x: 0,
    y: 0,
    // z: 0
}
Ken Browning
god, i wished IE just accepted this as valid.
Anurag
This is one of the few instances were C# is *less* strict than JavaScript!
Dan Diplo
@Dan and that's just for IE...Well you have to admit, with the var keyword in 3.0 C# has made huge strides in source code compatibility with javascript.
Min
Accepting this on Community judgement!, highest voted post will be accepted :)
Mahesh Velaga
To get around this I usually put the comma _before_ the element, and just don't put a comma on the first line.
Christian Mann
+4  A: 

Calling $(this) inside an ajax success.

Ben
+5  A: 

Forgetting semicolons at the end of curly brackets

var point = {
    x: 0,
    y: 0,
    // z: 0
} // <= semicolon goes here

var foo = function(){
  return 'foo!';
} // <= semicolon goes here

Plugin authors, don't forget!

(function($){
  // plugin code
})(jQuery); // <= look ma! semicolon!

This is pretty common, too

$("#foo")
  .find('a')
    .click(function(){
      // yay!
    })
  .end()
  // more chaining...
  .slideDown() // <= semicolon goes here

Forgetting the semicolon after the last statement in a function:

var foo = function(){
  linesOfCode();
  responsibleSemicolons();
  alert("happy") // <= semicolon goes here even though it works without one!
} // <= and here!

Note

This is not specific to jQuery, but applies to Javascript. Modern browsers are usually smart enough to get past this now. Javascript minifiers will typically correct this for you, too.

macek
Omitting the semicolon is allowed by the language.
erikkallen
@erikkallen But it's a bad idea - both omitting semicolons and the fact that the language allows you to get away with it. The way JavaScript handles missing semicolons is that, when it gets an error, it sticks one in and tries again. The problem is that it guesses where to insert them and sometimes gets it wrong. It's much better to just be explicit about where they should be.
Jimmy Cuadra
@Jimmy. Many Javascript minifiers also REMOVE the UNNECESSARY semicolons (it IS a minifier). I agree that it is a good habit to "punctuate" your javascript statements to avoid those confusing bugs...but i wouldn't consider it a mistake to leave them out. Heck, in Javascript 1.8 this is valid: `function(x,y)x*y` (same as `function(x,y){return x*y;}`).
David Murdoch
+2  A: 

Not the most common mistake I made, well I did it a couple of times then I learned the hard way. But this has got to be the most common mistake committed in jQuery of all time. There is at least one daily question on SO regarding this.

Cross-domain requests (with an XHR status of 0).

Anurag
A: 

I always mix up append(), prepend(), and after() for some reason. I think visit the "Manipulation" section of docs.jquery.com more than any other section because of this. After reading the docs it's always painfully obvious which one I should be using, but it never sticks.

Kevin Pang
+1  A: 

Because it's so easy to write $("#someid"), I often don't store the result in a variable if I need it again later - I just do $("#someid") again. Not really a mistake though.

zedoo
Its $("#someclass") not $(#someclass) there u go! :)
Mahesh Velaga
Its $(".someclass") not $("#someclass"). And if you aren't caching your queries...shame on you!
David Murdoch
I'd say that it is a mistake.
Tim Down
@Tim Down, +1. A HUGE mistake.
David Murdoch
jQuery 1.4 caches most DOM queries automatically, so it's less of a mistake now. It's definitely worth saving to a variable if you are modifying some parts of the DOM because jQuery might not cache it then.
DisgruntledGoat
A: 

Setting .attr('checked', true) on a node that isn't added to the DOM and expecting it to work on Internet Explorer. I ended up writing this workaround function:

jQuery.fn.setChecked = function(checked) {
    var that = this;

    if (checked !== undefined)
        checked = !!checked;
    else
        checked = true;

    /* Needed so attr('checked') will return true before
       the .ready callback is called. */
    that.attr('checked', checked);

    /* Needed so IE6 will actually check the checkbox
       in some cases. */
    that.ready(function(){
        that.attr('checked', checked);
    });
}

Either this is a bug report, or it is my mistake to manipulate a DOM node before it's ready. Nevertheless, it's worth posting anyway :-)

Joey Adams
+1  A: 

Writing

$(#notAVariable)

instead of

$('#notAVariable')

or

$("#notAVariable")
Adam
somebody doesn't use syntax highlighting.
David Murdoch
+6  A: 

I often write a whole lot of c# then come to javascript and write a for loop like this:

for (int i = 0;i < blah.length;i++)

grr

Alistair
Actually, this is exactly how you should loop over arrays in JavaScript. `for (var i in array)` is broken in JavaScript. If you (or a toolkit) adds members to Array.prototype, they show up in iterations of `for (var i in array)`. jQuery.each does the right thing.
Joey Adams
@Joey, I think what he means is that he says **int** instead of **var**. I do this too :)
harpo
+2  A: 

The biggest mistake I use is using $('#test') instead of jQuery('#test'). This causes a lot of conflicts with a lot of other javascript toolkits as $ is commonly used in them also.

If you want to use $ rather than writing jQuery all over the place, you can wrap your code in a function call, like so (note: this technique is ):

(function($){
    // $ can now be used for jQuery
})(jQuery);

Beware not to use $ for other JavaScript toolkits, as $ is set to jQuery inside the function. This works by creating a function, then calling it with jQuery (thus binding jQuery to $). Because of lexical scoping, the $ definition here won't interfere with the outside scope.

jini
I hate, hate, HATE it when plugin authors write every instance of `$` with `jQuery` when they should just use the closure method above.
David Murdoch
you can also use the noConflict function (http://api.jquery.com/jQuery.noConflict/) for these cases
GSto
+2  A: 

.before() and .after()

$("div").before("#someOtherDiv") 

actually moves #someOtherDiv from the DOM to before the new div. I always think "div" will be moved before #someOtherDiv.

Same goes for after().

Jeff Meatball Yang
introducing `insertBefore()`
David Murdoch
I know about insertBefore (and insertAfter), but it's hard to remember the difference between the two sets. The syntax leaves the question of where is the "target"? - in the root object, or in the function args?
Jeff Meatball Yang
There is also `append`, `appendTo`, `prepend` and `prependTo` to contend with as well.
DisgruntledGoat
@Goat: I am also disgruntled about those.
Jeff Meatball Yang
A: 

I'm usually typing too quickly, and end up with some gibberish like

#$("id")

or

$#("id")

or

$(#"id")

More a general JavaScript issue than jQuery. After writing a bunch of perl, I'll write:

for(my i = 0; i < max; i++)

Only to get yelled at about using my by the error console.

justkt
A: 

Whenever I use the two-argument toggleClass method, which adds or removes a class based on whether the second argument is true or false, I have to remind myself that if the second argument isn't really and truly a boolean value, then the method will behave as if it were called with just one argument, removing the class if it is present, and adding it if it's absent.

var obj = { };
var p = $("<p/>").toggleClass("hidden", obj.foo);

Now p does have a class="hidden" attribute, even though obj.foo is false in a boolean context. This leads to having to use defensive cruft like

var p = $("<p/>").toggleClass("hidden", !!obj.foo);
Sean