Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the necessasity of # in Jquery

Tags:

html

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 # ??

like image 236
Suresh Avatar asked Apr 29 '16 06:04

Suresh


People also ask

What is the full meaning of necessity?

Definition of necessity 1 : the quality or state of being necessary He questioned the necessity for the change. 2a : pressure of circumstance The plane was compelled by necessity to change its course. b : physical or moral compulsion did it, not because he wanted to, but by necessity.

What is a necessity example?

The definition of a necessity is something that is absolutely needed. An example of a necessity is water for life. Something necessary. The necessities of life include food, clothing, and shelter.

What noun is necessity?

1[uncountable] the fact that something must happen or be done; the need for something necessity (for something) We recognize the necessity for a written agreement.


1 Answers

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.

like image 79
Chris Avatar answered Oct 29 '22 09:10

Chris