Help with js script

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)

thx!

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.

edit: it doesn’t return an array but an array-like collection. https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName

i changed it, But it still does not work :confused:

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 :slight_smile:

i did a pen, but deleted it (works fine in codepen) the problem is on local… here is a link for dropbox

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.

is well linked, i tried an alert and worked fine.

and the msg in the console is “document is not defined” so i give it the name of the html but nothing :rage:

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.

2 Likes

thats it! thank you very much! i cant belive i spend hours trying to figure this out xD

1 Like

Yay!

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 :wink:

1 Like