I have a simple function which I want to run on all elements with a certain class. I want this function to be run as soon as the DOM is loaded; not upon an event handler.
Below is the code that will run on an event handler like hover. How do I make it run as soon as the DOM is loaded?
$(document).ready(function(){
var displayname = $(".displayname");
function alignSpans(){
var spanheight = this.offsetHeight;
var cssbottom = spanheight + "px";
this.style.bottom = cssbottom;
}
displayname.hover(alignSpans);
});
displayname.alignSpans(); does not work
Assuming
displaynameis a jQuery object, you can use.each()to call a callback function usingthisas the contained DOM elements (which is very conveniently the same convention as an event handler)