I have a form with radio buttons in my html and a script to trigger an alert but the script is causing an error stating that the function is undefined what am I missing is it because it’s in codepen and needs a different approach?
can’t say for sure unless all code is shown - could be your script and function runs before the page is fully loaded - it’s best to keep all js inside the page load event handler
document.addEventListener("DOMContentLoaded", ()=>{
// all DOM code in here
})
Its a twitch stream status app, I’ve tried several ways to trigger a javascript function in response to a radio button change to no avail. So I’m try the most basic way I could find.
Removing the $(document).ready(function(){} fixes it. Not sure why though. I guess the function that you link to in onchange="" should exist at the moment the DOM is being loaded?
Found the reason why it doesn’t work: scope. You define the function within the callback function of .ready(), so it is only available inside that function.
function test(){
var x = "Available inside this function";
console.log(x);
}
test(); // this will output: "Available inside this function"
console.log(x); // will output: ReferenceError: x is not defined
I understood the scope but not quite understand why the html call would be outside it. To me the html relation seems different to something that would be outside. I can’t find the right words to discribe it.
Ok thanks, It’s not pretty but It will work for the time being.