Jai Gtz
Jai Gtz

Reputation: 65

Different between $('#id') vs $(id)?

What difference between this two code? Because when i test it using chrome console it give return same value. Is this mean if not put '#' that mean it automatically assume it as for id?

Upvotes: 0

Views: 303

Answers (2)

Felix Kling
Felix Kling

Reputation: 816780

In JavaScript source code, foo is a variable, 'foo' is a string literal (representing the string value foo). Variables can hold string values:

var bar = 'xyz';

In this example the variable bar is assigned the string value xyz.

Therefore, $('#id') passes the string value #id to the $ function. $(id) on the other hand passes the value of the variable id to the function.

These are very different operations but of course they can have the same outcome.


I think you'd benefit the most from reading a JavaScript tutorial such as http://eloquentjavascript.net/ .

Upvotes: 2

Bindrid
Bindrid

Reputation: 3735

Look up jquery selectors at jquery.org. # in the string means the string is an id. ".classname" will use the class name to find it. Selectors can be very complicated so it will be worth your time to look it up.

Upvotes: 1

Related Questions