views:

92

answers:

4

Being fully self-taught without actually reading up on JavaScript (It's my job now, believe it or not) there are a few things I accept but don't understand.

The first one is the dollar sign.

As far as I use understand it, it's a shortcut to document.getElementById(), but if I log $ and document.getElementById() to console - Only $ returns a value. This value however is always function(), shouldn't it be. The element? What gives?

The second issue I have is something that keeps coming up in my code and I go out of my way to change the code to eliminate it. It's the "... is not a function" error.

For example:

if ($.inArray($(div_id).val(), arr) >= 0);

Will give the error .val() is not a function. Why? And how do I use the value of div_id to see if it's in array?

A: 

$ and document.getElementById is not one of the same thing. $ gives you a function in console only when you are using some library like jquery which mapes $ to a function.

.val id primarly used to get value of the form elements and that is a jquery function. I think you need to learn more around javascript and jQuery

sushil bharwani
+4  A: 

Hiya. When you're using Jquery (which I assume you are), then $ will return the jquery object. This can contain an array of matched HTML elements depending on the selector you used. For example $("#foo") will return the jquery object containing the element with id foo. You can get the actual HTML DOM element out using $("#foo")[0] - using the array-style notation.

Can you give us some more info on what you're trying to achieve with the $.inArray example?

Ben Clayton
jQuery doesn't alias `$` to `document.getElementById`, it aliases `$` to its CSS selector query engine (which used to be internal, and now uses Sizzle). Using `$` as an alias to `document.getElementById` is probably a hint that the questioner is using either Prototype or Mootools or a similar library (these actually do more than select the element with that id, for that matter).
eyelidlessness
@eyelidlessness: Actually, jQuery it aliases `$` to `jQuery`, which is a function that does more than just use the selector engine, depending on what you pass into it.
T.J. Crowder
@T.J. Crowder, that's correct. I was being overly concise due to comment character limi…
eyelidlessness
Yes it is jQuery, the confusion with getElementById is based on a short and rather ignorant search for an answer. Having no "fundamental" training, I treat jQuery and JS roughly as the same thing as they're what I work with. What I'm trying to achieve is to find a match between a value in a selection list and an array (Which currently looks horrid due to trying to fit in JSON objects into it....)
M.E
To be more specific - I'm gathering up data from markers placed on a google map in an array (Which I'm struggling with) as it needs location name, an id, latitude, longitude and a category, I then need to compare the selected name (id) with the markers in the array.
M.E
+2  A: 

$ is a valid variable name.

So if you try to use $ without setting it, it will not work.

A lot of people/frameworks however use $ as a shortcut to document.getElementById, they would declare it at the top of the script as:

function $(id) { return document.getElementById(id); }
Tom Gullen
A: 

Neither Javascript nor the DOM define $, which (as other answerers said) is often defined in general-purpose DOM libraries like jQuery, Prototype or Mootools. Based on the particular code you included, I suspect you've been coding against the jQuery API (because you use $.inArray, see http://api.jquery.com/jQuery.inArray/; though your claim that $ aliases document.getElementById confuses matters, as jQuery expects CSS selectors rather than element IDs).

When $ is expected but undefined, that usually means you'll need to include the library whose API you're using in the HTML document.

eyelidlessness