What's the jQuery equivalent for each():
$(".element").each(function(){ // do stuff });
when attaching a function to a single element, like #element
?
$() = window. jQuery() $()/jQuery() is a selector function that selects DOM elements. Most of the time you will need to start with $() function. It is advisable to use jQuery after DOM is loaded fully.
In jQuery, you can listen to events for dynamically added elements using the on() function. The equivalent in JavaScript is addEventListener() function.
each() function, jQuery's foreach equivalent. jQuery's foreach equivalent can be very useful for many situations. These examples will get you started and teach you how you can loop through arrays, objects and all kinds of HTML elements.
The $() function The dollar function, $(), can be used as shorthand for the getElementById function. To refer to an element in the Document Object Model (DOM) of an HTML page, the usual function identifying an element is: document. getElementById("id_of_element").
You can always reference the jQuery object in a variable:
var $el = $('#element');
...then manipulate it.
$el.doSomething(); // call some jQuery methods from the cached object $el.doSomethingElse();
If the reason you wanted .each()
was to reference the DOM element as this
, you don't really need the this
keyword to do it, you can simply grab the DOM element out of the jQuery object.
var element = $('#element')[0]; // both of these give you the DOM element var element = $('#element').get(0); // at index 0
The two of these are equivalent, and will retrieve the DOM element that would be referenced as this
in the .each()
.
alert( element.tagName ); // alert the tagName property of the DOM element alert( element.id ); // alert the ID property of the DOM element
I'd note that it isn't necessarily bad to use each to iterate over a single element.
The benefits are that you have easy access to the DOM element, and you can do so in a new scope so you don't clutter the surrounding namespace with variables.
There are other ways to accomplish this as well. Take this example:
(function( $ ) { // Inside here, "this" will refer to the DOM element, // and the "$" parameter, will reference the jQuery library. alert( this.tagName ); // Any variables you create inside will not pollute the surrounding // namespace. var someVariable = 'somevalue'; // is local to this function }).call( $('#element')[0], jQuery );
To directly answer your question, .each()
operates normally on element sets of any size including 1.
You can also omit the .each()
call completely and just call jQuery methods on $('#element')
. Remember that you can chain most if not all jQuery method calls as they return the jQuery object. This even works on multiple elements for the matter, depending on what the methods do.
$('#element').doSomething().doSomethingElse();
If you need to reference the object multiple times, make a variable:
var $elm = $('#element'); $elm.doSomething(); doSomethingElse($elm);
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