so I have the following code
$(document).ready(function(){
//enable submit button once user makes a radio button selection
if (document.querySelector('input[name="benefitType"]')) {
document.querySelectorAll('input[name="benefitType"]').forEach((elem, i) => {
elem.addEventListener("change", function(event) {
document.querySelector('#benefit-quote-submit').disabled = false;
});
});
}
basically, I am enabling the submit button (of type <button) when one of the radio buttons in the form is selected.
I want to replace $(document).ready with normal Javascript equivalents.
I tried
if(document.readyState === 'ready' || document.readyState === 'complete') {
doSomething();
but it’s not working (when I said it’s not working, my breakpoint on this line "elem.addEventListener" is not breaking the execution. It works fine though with $(document).ready.
Thank you.
3
Answers
So you want to do some action when the website is loaded.
You can use the onload event on the body:
Or you can achieve the same by adding an event listener:
I hope this helps!
To replace $(document).ready with normal JavaScript equivalents, you can use the DOMContentLoaded event.
Here’s an example of how you can modify your code:
Happy coding:)
The document/DOM is ready when the DOMContentLoded event happens on the document.
So here is a complete example of how the button can be enabled when the form is valid. There is no reason to have event listeners on each input. When something happens in the form you can just test using checkValidity() on the form.