Hi! i’m doing a course at udemy to understand json better, and in the very first chapter i cant put this script to work… i’m so frustrated because i know is probably some very simple mistake, but i cant fix it… this is the code:
js:
html:
the idea is to modify de div with the class “output”, but nothing happes (but it works on codepen)
I think the problem might be that you are using ‘getElementsByClassName’ to select the button. Try using ‘getElementById’ instead. ‘getElementsByClassName’ returns an array, not an object and that could be why your function isn’t working.
hmm, it seems to be working for me. can you link you codepen? Also, when asking for help it is always best to link to your code, or include it in the original post
Well, without seeing the code I would say the issue is either a typo or that the js file is linked incorrectly. Did you test the onclick function with a console.log? You could also throw in a console.log(“js file linked”); into the js file to make sure the browser is loading it.
I would wager the code worked on Codepen because it didn’t need to be linked. You could also try the js code within a tag on the html file to see if it is working.
Are you linking the JS script after the body? If it’s linked in the header then what you are assinging hasn’t been created yet, thus throwing the errors.
I have been stuck for multiple hours on minor syntax errors so I know the feeling well. On the plus side, I highly doubt you will ever make this error again