-3

I was looking at this question: The preferred way of creating a new element with jQuery

In it, they mention these two methods of using jquery:

Either:

var div = $("<div></div>");
$("#box").append(div);

Or:

$("#box").append("<div></div>");

I'm looking at this, and noticing that if you put actual HTML in the $ function, it acts like you are creating elements in memory. But if you use an HTML selector, it's referring to elements on the document? At least that's my interpretation. I'd like to find all the ways i can use the $ function, but when I search the site, I don't know how to find it.

Can someone list all the ways I can use $, and if possible, links to the official documentation?

Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356
  • 1
    Have you been over `api.jquery.com`? I mean, that is the jQuery official documentation. – Pointy Jul 12 '20 at 20:24
  • 1
    @Pointy yes... I even linked to it in my question. – Daniel Kaplan Jul 12 '20 at 20:25
  • 1
    Well its literally all right there. Look at the overall documentation to the jQuery function itself, and that will explain how it interprets the arguments it's passed by checking the number and types. – Pointy Jul 12 '20 at 20:26
  • 1
    why is this being downvoted? it's a legitimate question. Humans weren't innately born with the knowledge that `$` is a shortcut to `jQuery()`. – Daniel Kaplan Jul 12 '20 at 20:27
  • 3
    Well "Where can I find documentation for X" is literally one of the "Close" reasons for a question. – Pointy Jul 12 '20 at 20:27
  • 2
    "I looked at that documentation and am still confused" is a bit blurrier of a line, @Pointy. – ceejayoz Jul 12 '20 at 20:29
  • 1
    Thank you, @ceejayoz In fact, I'm still not sure how I could figure this out from the documentation. It would be nice to know where it says that this is a shortcut. It's definitely not on the api homepage, and when you search for $ on their website, it only shows you "$.Callback" as a result. – Daniel Kaplan Jul 12 '20 at 20:30
  • @DanielKaplan I guess part of it might be that like 10 years ago not knowing that `$` is a synonym for the main jQuery function would have seemed odd, but so much has changed since then. I'm not sure how much sweeping "Gee lets update all of this" activity there's been in that community. – Pointy Jul 12 '20 at 21:02
  • It's certainly not very stridently announced in the docs, but almost all the examples at the jQuery site use `$()`. It's after the first block of "blue" API notes on the `jQuery` function page that a paragraph mentions in passing that jQuery is aliased by `$` – Pointy Jul 12 '20 at 21:05
  • @Pointy so... if you know exactly where to look, it's easy to find? Or are you telling me how to find the information? – Daniel Kaplan Jul 12 '20 at 21:07
  • 2
    No no, I was basically agreeing with you: you look up the basic jQuery function, and if you miss a single phrase then the whole page of examples is like "what?" e: and not just that page, like essentially the entire rest of the API docs – Pointy Jul 12 '20 at 21:10
  • Please don't answer questions in comments. – pppery Jul 13 '20 at 01:37
  • 1
    Does this answer your question? [What does the "$" sign mean in jQuery or JavaScript?](https://stackoverflow.com/questions/8667736/what-does-the-sign-mean-in-jquery-or-javascript) – jonrsharpe Jul 13 '20 at 06:35
  • @jonrsharpe doesn't look like it to me. that tells you what the $ means, but not how to use it in every possible way. It also does not explain how to learn more – Daniel Kaplan Jul 13 '20 at 07:46
  • @DanielKaplan once you know what is, you can find it in the site you were already looking at. Also the answers to that and its own dupe contain links to various other resources. *"how to use it in every possible way"* isn't an appropriate scope for SO, that's what docs are for. – jonrsharpe Jul 13 '20 at 07:52
  • 1
    @jonrsharpe Feel free to close it if you feel that way. I find the answer below to be satisfactory for what I was looking for and I don't feel that way about the one you consider a dupe. But I already got my answer. – Daniel Kaplan Jul 13 '20 at 07:56

1 Answers1

2

$() is a shortcut for the jQuery() function, and you can find the documentation for it here: https://api.jquery.com/jQuery/

Multiple argument types are accepted, including selector, element, elementArray, object, and callback types. From the docs:

Argument types for jQuery().

Lewis
  • 4,285
  • 1
  • 23
  • 36
ceejayoz
  • 176,543
  • 40
  • 303
  • 368
  • 2
    Who would downvote this obviously correct answer? In my opinion the question should be closed as too broad anyway; I mean there are few easier tasks on the internet than locating jQuery documentation. – Pointy Jul 12 '20 at 20:25
  • 3
    The question is asking for offsite resources. Off-topic. Should be closed, not answered. – trincot Jul 12 '20 at 20:26
  • 1
    Sure there are many difficult things for newbies. But that doesn't change what is on or off topic here. "difficult" is not the criterion. – trincot Jul 12 '20 at 20:29
  • 2
    @trincot As I read it, OP is essentially not asking for an offsite resource (given that they link to it in the question itself), but understanding something confusing *in* that resource. – ceejayoz Jul 12 '20 at 20:36
  • 1
    @ceejayoz The kicker here is the question, as stated, was literally "where's the documentation for `$`", but there was also an implied question about "how does jQuery behave depending on the argument type" and an element of *understanding* the docs like you mentioned. I downvoted the OP, but I was in the middle of an answer offering more context than just a link when it was closed. – Lewis Jul 12 '20 at 20:38