r/JavaScriptTips • • Feb 08 '21

Problem with click element, syntax error maybe? Sorry 4 bad quality...

Post image
3 Upvotes

8 comments sorted by

4

u/classy_variable Feb 09 '21 edited Feb 09 '21

When you’re selecting an element by id using getElementById you shouldn’t use the # identifier there. You already specify the argument is an id because of the method name. It should be document.getElementById(“sub”)

2

u/Khabibulix Feb 09 '21

Hello classy_variable, argh you're right I missed that point. I redo it but... Alas it didn't work as I wanted sigh...

2

u/classy_variable Feb 09 '21

There are a few other problems with your syntax. I'd take a look at W3 schools for guidance. Here is a a good example of different ways to handle onClick events: https://www.w3schools.com/jsref/event_onclick.asp

2

u/Khabibulix Feb 09 '21

Oh holy cross! It worked!! I understand my mistake. I humbly Ty.

3

u/DownRyte Feb 08 '21

If this is a HTML (which it looks like it is) then you can’t have the code that is on and after line 27 there. You need to either put that code in between script tags before the end of the body or put It in a file named “script.js” and make 8 self closing. Feel free to PM me if that didn’t make sense.

1

u/Khabibulix Feb 09 '21

Hello and thanks for your fast answer, in fact it is in a script.js but I copy paste in the HTML file to make it more...readable 😏

1

u/TeslaRanger Feb 09 '21

The <h1> stuff in the <head> section belongs down in the <body> section too, and pay attention to indenting and more importantly to your syntax coloring starting with the closing script tag.

2

u/Khabibulix Feb 09 '21

Ahoy, TeslaRanger! Hope you'll have a nice day, I moved the h1 as u said and indented like a pro. I think the baaad color comes from the fact that I copy paste html in my script.js making it clumsy... Thanks again, my code is prettier now!