-1

I have seen code online using this operator and I don't know what it's supposed to mean:

var div1  =$  ('#div1');
var div2  =$  ('#div2');
var div3  =$  ('#div3');

It's returning me the element with the corresponding IDs, but why? How does it work and is it native?

VXp
  • 11,598
  • 6
  • 31
  • 46
  • 1
    I think that's just jquery but formatted like an operator, for reasons unknown. – Phiter May 14 '18 at 18:48
  • 5
    Nothing. Bad code formatting by the developers. `var div1 =jQuery (selector)` – Taplar May 14 '18 at 18:48
  • Exactly, you can place spaces between most code in js as you like, unless you are mixing two variable names. – Luca Kiebel May 14 '18 at 18:49
  • @Taplar or to be more precise for this particular case `var div1 = $('#div1');` – VLAZ May 14 '18 at 18:50
  • Related: [What is the "-->" operator in C++?](https://stackoverflow.com/questions/1642028/what-is-the-operator-in-c) – Frax May 14 '18 at 18:52
  • It means `var div1 = $('#div1')` , so the function $ could be `function $(qs) { return document.querySelector(qs) ; }` – user3094755 May 14 '18 at 18:59

5 Answers5

8

It means:

var div1 = $('#div1');

Just poor formatting by whoever wrote that.

Kurt
  • 1,868
  • 3
  • 21
  • 42
  • 2
    This is also a pretty good example of why you should consider how you format your code, especially if your code is going to be worked on by multiple developers. Which is also why there exists software that will format your code to a shared group standard before you commit, so you can write to whatever standard you like, but it gets stored in a format that everyone doesn't have to do a double take on to figure out wtf. – Taplar May 14 '18 at 18:54
  • 1
    Worth noting it's a jquery object for those who don't know :) – treyBake May 14 '18 at 20:37
0

Looks like the spacing is just not good. It is actually

var div = $('#div1') 

a jQuery selector for element with id div1

Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
iStepashka
  • 302
  • 1
  • 6
0

It is not correct formatting. Мore correctly write like this:

var div1 = $('#div1')

The jQuery library exposes its methods and properties via two properties of the window object called jQuery and $. $ is simply an alias for jQuery and it's often employed because it's shorter and faster to write.

In plain JS you can write:

var div1 = document.getElementById("div1");
Sergey Fedorov
  • 3,696
  • 2
  • 17
  • 21
0

Its basically selecting the divs like $('#div1'), $('#div1') so on, just formatted wrongly. That is why you are receiving element with the corresponding IDs

sam
  • 31
  • 7
0

This is id of one of the div element in HTML code, Which can be used to define actions on that div element like:

var div1 = $('#div1');
div1.show();
div1.empty();
Aarif1430
  • 155
  • 9