error

whats the solotion for this error
No description
No description
No description
67 Replies
Bardacoon
Bardacoon16mo ago
Are you sure that you can add an onclick to a button like that?
laith
laithOP16mo ago
wdym?
Bardacoon
Bardacoon16mo ago
You usually add an addEvenListener
laith
laithOP16mo ago
still dont understand u mean i have to use <button>?
Bardacoon
Bardacoon16mo ago
document.getElementById("button").addEventListener('click', () => {...})
document.getElementById("button").addEventListener('click', () => {...})
laith
laithOP16mo ago
do u know bro code? on youtube
Bardacoon
Bardacoon16mo ago
No
laith
laithOP16mo ago
No description
laith
laithOP16mo ago
look hese on youtube js course he did it liek this and it worked nvm so where do i put the function?
Bardacoon
Bardacoon16mo ago
instead of this function also remember to add "" to the welcome in your code
laith
laithOP16mo ago
but i copied the same thing he did and it didint work
Bardacoon
Bardacoon16mo ago
can you send me the video? with the timing
laith
laithOP16mo ago
ofc gime a sec
laith
laithOP16mo ago
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...
laith
laithOP16mo ago
24:42
Bardacoon
Bardacoon16mo ago
I noticed he's using a real button not an input button
laith
laithOP16mo ago
should i try and tell u waht happend?
Bardacoon
Bardacoon16mo ago
and also place your script tag right above the </body> Ig
laith
laithOP16mo ago
ok ill just do the one u send me but like where do i put the things that happen when clicking it didint work
Bardacoon
Bardacoon16mo ago
can you osend me this code as text? what do you mean?
laith
laithOP16mo ago
document.getElementById("button").addEventListener('click', () => {alert("hi")}) like this?
Bardacoon
Bardacoon16mo ago
yeah like this but also when sending code here write 3 of these ```, then the code and again those backticks
laith
laithOP16mo ago
how u send it in text liek this document.getElementById("button").addEventListener('click', () => {...})
Bardacoon
Bardacoon16mo ago
No description
laith
laithOP16mo ago
nvm it works now thank you so much
Bardacoon
Bardacoon16mo ago
can you still send me the broken code from before? I wanna try to find the error
laith
laithOP16mo ago
kk gime a sec wait so how do i send it in text?
Bardacoon
Bardacoon16mo ago
just write it here in discord, but write it like the image i sent you earlier
Bardacoon
Bardacoon16mo ago
No description
laith
laithOP16mo ago
code
code
Bardacoon
Bardacoon16mo ago
adding js after the 3 backticks creates the color syntax for the code
laith
laithOP16mo ago
ok now gime a sec
Bardacoon
Bardacoon16mo ago
exactly, but instead of code add your own code
laith
laithOP16mo ago
btw only js or html also?
Bardacoon
Bardacoon16mo ago
html, js, css, ts, and many other
laith
laithOP16mo ago
huh no i mean what do i send u
Bardacoon
Bardacoon16mo ago
oh sorry haha both
laith
laithOP16mo ago
ok
console.log("hellow world");
let hellow;
hellow = welcome
document.getElementById("button").onclick = function(){
alert(welcome)
}
console.log("hellow world");
let hellow;
hellow = welcome
document.getElementById("button").onclick = function(){
alert(welcome)
}
this js opps
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>js page</title>
<link rel="stylesheet" href="hi.css">
</head>
<body>
<script src="hi.js"></script>
<input type="text" id="text">
<button type="button" id="button"></button>
<script src="hi.js"></script>
</body>
</html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>js page</title>
<link rel="stylesheet" href="hi.css">
</head>
<body>
<script src="hi.js"></script>
<input type="text" id="text">
<button type="button" id="button"></button>
<script src="hi.js"></script>
</body>
</html>
anything else bc igtg sleep i hav school
Bardacoon
Bardacoon16mo ago
You're using <script src="hi.js"></script> 2 times, remove the one above. And also when you say
let hellow;
hellow = welcome
let hellow;
hellow = welcome
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
let hellow = "welcome"
let hellow = "welcome"
This (vvv) is not wrong, it's just weird
let hellow
hellow = "welcome"
let hellow
hellow = "welcome"
curator
curator16mo ago
do this:
document.getElementById("button")?.onclick
document.getElementById("button")?.onclick
add a question mark before .onclick
Bardacoon
Bardacoon16mo ago
Yeah, also that
laith
laithOP16mo ago
yay now i have a new error
Bardacoon
Bardacoon16mo ago
That basically means, if document.getElementById("button") is true (basically means, if it exists) run the onclick else, stop send the code and error
laith
laithOP16mo ago
No description
laith
laithOP16mo ago
the code is the same but with the ?
Jochem
Jochem16mo ago
yeah, this doesn't work in assignment cases
laith
laithOP16mo ago
so waht this error means?
Bardacoon
Bardacoon16mo ago
it says there's something wrong at line 4, what's line 4 tho
Jochem
Jochem16mo ago
that you can't use the null safe property accessor (?.) on the left side of an assignment operator (=)
laith
laithOP16mo ago
document.getElementById("button")?.onclick = function(){ this so i remove the "="? nvm am dum
Bardacoon
Bardacoon16mo ago
no, remove the ?, listen to @Jochem (i think)
laith
laithOP16mo ago
dam
Jochem
Jochem16mo ago
remove the ? yeah
laith
laithOP16mo ago
now it works smh
Jochem
Jochem16mo ago
which script tag did you remove?
laith
laithOP16mo ago
wdym? the one in the top
4ngel_._
4ngel_._16mo ago
? is optional chaining
Bardacoon
Bardacoon16mo ago
he had 2 script tags
<script src="hi.js"></script>
<input type="text" id="text">
<button type="button" id="button"></button>
<script src="hi.js"></script>
<script src="hi.js"></script>
<input type="text" id="text">
<button type="button" id="button"></button>
<script src="hi.js"></script>
laith
laithOP16mo ago
all i did was ading then removing the "?" then it works like how
Jochem
Jochem16mo ago
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
laith
laithOP16mo ago
ohhhhhhhh i always put my script upper thats why so every thing was bc of the script tag
curator
curator16mo ago
adding a safe check will resolve i guess
Jochem
Jochem16mo ago
not everything, the other things Bardacoon pointed out were still also good points
laith
laithOP16mo ago
now i can contenue js after 2 weeks of stucking at this error yay
Bardacoon
Bardacoon16mo ago
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)
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
laith
laithOP16mo ago
mhm thx every one oh that will help alot now i gtg sleep thx and bye👋
snxxwyy
snxxwyy16mo ago
i'd recommend mdn not w3schools, w3schools has a lot of bad practices and outdated methods.
Want results from more Discord servers?
Add your server