Inside “myFunction”, I have an each() iterator which finds the height of certain elements. How can I assign each height value to an incrementally generated variable name, accessible outside the each loop?
function myFunction(){
$('#div1, #div2 ... #divN').each(function(index){
/* Increment variable name here */ = $(this).outerHeight();
});
// Also use those variables here
};
Thanks.
This task is better solved with an array, and thanks to jQuery’s built-in methods, you can easily extract certain values from a set of elements to an array:
I have to stress that the order of the
heightsarray is not the order in which you put the elements in the selector! jQuery will always reorder the elements to how they are positioned in the DOM.Reference:
.map,.getIf you want the height somehow associated to something which can identify the element, you can use an object where the keys are the element IDs and the values are the heights: