I'm a bit confused regarding the dollar sign in jQuery, and was hoping someone could help me out.
I have the following function declaration:
$(function() {
$( "#create-discussion" ).button().click(function() {
alert("Clicked");
});
$( "#listitems tr" ).click(function(event) {
alert("clicked");
});
});
For some reason, the first function declaration for the "create-discussion" button works perfectly; when clicked, a popup appears. However, the second one does not work, and no popup is generated when I click on the table rows.
Is this some nuance in regards to button onClicks versus table row onClicks? Am I missing something stupidly obvious?
Also, I think it would help a bunch if someone explained what $(function() {})
actually does, as I'm treating it like $(document).ready()
, and I'm not sure if I can do that.
Normally in your code you can use $ as a replacement for "jQuery". If you use noConflict() you can't do that anymore and so you have to replace each "$" with "jQuery"; . This works, just want to point out that the first argument passed to a ready callback is the jQuery object, so you can do .
What does dollar sign ($) means in jQuery ? The $ sign is nothing but an identifier of jQuery() function. Instead of writing jQuery we simply write $ which is the same as jQuery() function. A $ with a selector specifies that it is a jQuery selector.
In jQuery, the $ sign is just an alias to jQuery() , then an alias for a function. This page reports: Basic syntax is: $(selector).action() A dollar sign to define jQuery. A (selector) to "query (or find)" HTML elements.
The dollar sign ($) and the underscore (_) characters are JavaScript identifiers, which just means that they identify an object in the same way a name would. The objects they identify include things such as variables, functions, properties, events, and objects.
A dollar sign ($
) is actually an alias for jQuery
function. And according to the documentation, if you pass a callback as an argument to this function, it will be executed when the DOM is ready.
When it comes to the second part of your question (about why the second part of the code is not working): just check the selectors. For me it is working perfectly (see jsfiddle - it is without .button()
method, because I am not loading jQuery UI), so this may be caused by incorrect selectors.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With