Help with js script

Help with js script
0

#1

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!


#2

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


#3

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


#4

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:


#6

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


#7

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.


#8

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


#9

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


#10

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.


#11

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


#12

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: