Suresh
Suresh

Reputation: 923

What is the necessasity of # in Jquery

If my html is as like below

<div id="TestHash"> </div>

I can access it as $("#TestHash") , $(TestHash). Both will result the same. what is the usage of element without # ??

Upvotes: 7

Views: 91

Answers (1)

Chris
Chris

Reputation: 58192

In fact, there is a legacy artefact in Javascript - elements with an ID automatically populate global namespace.

This is why you can access it directly the way you are ($(TestHash)). However, it's a misunderstood piece left over from legacy browsers, so you shouldn't rely on it.

Where possible, always use the $("#myId") version.

Upvotes: 6

Related Questions