67 Replies
Are you sure that you can add an
onclick
to a button like that?wdym?
You usually add an
addEvenListener
still dont understand
u mean i have to use <button>?
do u know bro code?
on youtube
No
look
hese on youtube
js course
he did it liek this and it worked
nvm so where do i put the function?
instead of this function
also remember to add
""
to the welcome
in your codebut i copied the same thing he did and it didint work
can you send me the video?
with the timing
ofc gime a sec
Bro Code
YouTube
JavaScript Full Course for free 🌐 (2023)
#JavaScript #tutorial #beginners
JavaScript tutorial for beginners full course
⭐️Time Stamps⭐️
#1 (00:00:00) JavaScript tutorial for beginners 🌐
#2 (00:08:02) variables ❎
#3 (00:16:32) arithmetic expressions 🧮
#4 (00:21:14) user input 🧮
#5 (00:25:41) type conversion 💱
#6 (00:30:11) const ⛔
#7 (00:33:13) Math 🔣
#8 (00:36:52) hypotenuse...
24:42
I noticed he's using a real button not an input button
should i try and tell u waht happend?
and also place your script tag right above the
</body>
Igok
ill just do the one u send me
but like where do i put the things that happen when clicking
it didint work
can you osend me this code as text?
what do you mean?
document.getElementById("button").addEventListener('click', () => {alert("hi")})
like this?
yeah like this
but also when sending code here write 3 of these ```, then the code and again those backticks
how u send it in text liek this document.getElementById("button").addEventListener('click', () => {...})
nvm it works now
thank you so much
can you still send me the broken code from before? I wanna try to find the error
kk gime a sec
wait so how do i send it in text?
just write it here in discord, but write it like the image i sent you earlier
adding
js
after the 3 backticks creates the color syntax for the codeok now gime a sec
exactly, but instead of
code
add your own codebtw only js or html also?
html, js, css, ts, and many other
huh
no i mean what do i send u
oh sorry haha
both
ok
this js
opps
anything else bc igtg sleep i hav school
You're using
<script src="hi.js"></script>
2 times, remove the one above. And also when you say
There's an error because Javascript now sees welcome
as a variable, a variable that is not defined. This happens because you need to make it "welcome"
, surrounded by "
. And also it doesn't really make sense to create a blank variable and then give it a value, just write
This (vvv) is not wrong, it's just weird
do this:
add a question mark
before .onclick
Yeah, also that
yay now i have a new error
That basically means, if
document.getElementById("button")
is true (basically means, if it exists) run the onclick
else, stop
send the code and errorthe code is the same but with the ?
yeah, this doesn't work in assignment cases
so waht this error means?
it says there's something wrong at line 4, what's line 4 tho
that you can't use the null safe property accessor (
?.
) on the left side of an assignment operator (=
)document.getElementById("button")?.onclick = function(){
this
so i remove the "="?
nvm am dum
no, remove the
?
, listen to @Jochem (i think)dam
remove the ? yeah
now it works smh
which script tag did you remove?
wdym?
the one in the top
?
is optional chaininghe had 2 script tags
all i did was ading then removing the "?" then it works like how
that error was happening because you had a script tag loading your script in your HTML before your button. The button didn't exist yet when the code was running. When you put the script tag at the end, then the browser already knows about all the other elements in the page and that makes it so that document.getElementById can find the button
ohhhhhhhh
i always put my script upper
thats why
so every thing was bc of the script tag
adding a safe check will resolve i guess
not everything, the other things Bardacoon pointed out were still also good points
now i can contenue js after 2 weeks of stucking at this error
yay
You can put the script tag wherever you want if you add
defer
https://www.w3schools.com/tags/att_script_defer.asp (it's recommended placing the script tag in the header tho)mhm
thx every one
oh
that will help alot
now i gtg sleep
thx and bye👋
i'd recommend mdn not w3schools, w3schools has a lot of bad practices and outdated methods.