There is this jQuery which seems to look for all elements with id="scuba"
, but then it uses attr() to pull the id out? Could "scuba" be part of the id and attr pulls the entire id out? I've never seen the $ inside an attribute selector, just outside like the example below.
$('*[id$=scuba]').attr('id')
So my questions are:
$
or $=
do in this exampleThe $ 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.
From Rick Strahl's description: The jQuery Object: The Wrapped Set: Selectors return a jQuery object known as the "wrapped set," which is an array-like structure that contains all the selected DOM elements.
jQuery selectors allow you to select and manipulate HTML element(s). jQuery selectors are used to "find" (or select) HTML elements based on their name, id, classes, types, attributes, values of attributes and much more. It's based on the existing CSS Selectors, and in addition, it has some own custom selectors.
The dollar sign
The first $
is a shorthand for the jQuery()
function, the jQuery object constructor.
In other words, it's a variable called $
that's been assigned a function called jQuery
, as can been seen in the unminified version of the jQuery source: window.jQuery = window.$ = jQuery;
The dollar-equals sign
The second $
is part of a jQuery selector called Attribute Ends With Selector . When used in an attribute selector, $=
is a logical operator that literally means "true if the left-hand value ends with the right-hand value".
What this script actually does
Overall, this snippet first selects any element with an id
attribute ending in scuba
. It then retrieves the id
value of the first element from the resulting jQuery object.
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