If I have functions that modify DOM elements on the page, but the $(document).ready() also access the same DOM elements, Do I wrap everything, including the functions in the $(document).ready() so I don’t declare dom elements multiple times. For example:
function addContent(){
var $someElement = $("someElement");
//Do something with $someElement
}
$(document).ready(function(){
var $someElement = $("someElement");
$someElement.click(function(){ //some code });
})
Would I move the addContent() function between the $(document).ready() so that I only have to declare $someElement once?
addContent()only needs to be outside of the$(document).ready(function(){})if it is needed on the global scope. The same goes for the selector.I personally wouldn’t have either of them in the global scope if at all possible, meaning no
onclick=attributes.