-2

I need to know how to write $(function() {...} in vanilla JS. However, bc it looks like Google doesn't let you search the dollar sign, I can't find it. I do know how to do $(document).ready(function{...}) in vanilla, but how does one do $(function() {...} in vanilla js?

1
  • do you mean getting to select elements like so? $(element)? @AviG Oct 30, 2020 at 20:03

1 Answer 1

-2
function stuffToDo() { ... }

// If we're already past the "DOM is ready" point, execute immediately:
if (document.readyState === "interactive" || document.readyState === "complete") {
  stuffToDo();
}

// Otherwise, wait for DOMContentLoaded to fire:
document.addEventListener("DOMContentLoaded", stuffToDo, { once: true });
2
  • this question is a dupe
    – dippas
    Oct 30, 2020 at 20:01
  • I don't think that justifies downvoting the answer; if you want to downvote the question, go ahead.
    – Domenic
    Oct 30, 2020 at 20:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.