error

whats the solotion for this error
No description
No description
No description
67 Replies
Bardacoon
Bardacoon10mo ago
Are you sure that you can add an onclick to a button like that?
laith
laith10mo ago
wdym?
Bardacoon
Bardacoon10mo ago
You usually add an addEvenListener
laith
laith10mo ago
still dont understand u mean i have to use <button>?
Bardacoon
Bardacoon10mo ago
document.getElementById("button").addEventListener('click', () => {...})
document.getElementById("button").addEventListener('click', () => {...})
laith
laith10mo ago
do u know bro code? on youtube
Bardacoon
Bardacoon10mo ago
No
laith
laith10mo ago
No description
laith
laith10mo ago
look hese on youtube js course he did it liek this and it worked nvm so where do i put the function?
Bardacoon
Bardacoon10mo ago
instead of this function also remember to add "" to the welcome in your code
laith
laith10mo ago
but i copied the same thing he did and it didint work
Bardacoon
Bardacoon10mo ago
can you send me the video? with the timing
laith
laith10mo ago
ofc gime a sec
laith
laith10mo 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
laith10mo ago
24:42
Bardacoon
Bardacoon10mo ago
I noticed he's using a real button not an input button
laith
laith10mo ago
should i try and tell u waht happend?
Bardacoon
Bardacoon10mo ago
and also place your script tag right above the </body> Ig
laith
laith10mo 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
Bardacoon10mo ago
can you osend me this code as text? what do you mean?
laith
laith10mo ago
document.getElementById("button").addEventListener('click', () => {alert("hi")}) like this?
Bardacoon
Bardacoon10mo ago
yeah like this but also when sending code here write 3 of these ```, then the code and again those backticks
laith
laith10mo ago
how u send it in text liek this document.getElementById("button").addEventListener('click', () => {...})
Bardacoon
Bardacoon10mo ago
No description
laith
laith10mo ago
nvm it works now thank you so much
Bardacoon
Bardacoon10mo ago
can you still send me the broken code from before? I wanna try to find the error
laith
laith10mo ago
kk gime a sec wait so how do i send it in text?
Bardacoon
Bardacoon10mo ago
just write it here in discord, but write it like the image i sent you earlier
Bardacoon
Bardacoon10mo ago
No description
laith
laith10mo ago
code
code
Bardacoon
Bardacoon10mo ago
adding js after the 3 backticks creates the color syntax for the code
laith
laith10mo ago
ok now gime a sec
Bardacoon
Bardacoon10mo ago
exactly, but instead of code add your own code
laith
laith10mo ago
btw only js or html also?
Bardacoon
Bardacoon10mo ago
html, js, css, ts, and many other
laith
laith10mo ago
huh no i mean what do i send u
Bardacoon
Bardacoon10mo ago
oh sorry haha both
laith
laith10mo 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
Bardacoon10mo 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
curator10mo ago
do this:
document.getElementById("button")?.onclick
document.getElementById("button")?.onclick
add a question mark before .onclick
Bardacoon
Bardacoon10mo ago
Yeah, also that
laith
laith10mo ago
yay now i have a new error
Bardacoon
Bardacoon10mo 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
laith10mo ago
No description
laith
laith10mo ago
the code is the same but with the ?
Jochem
Jochem10mo ago
yeah, this doesn't work in assignment cases
laith
laith10mo ago
so waht this error means?
Bardacoon
Bardacoon10mo ago
it says there's something wrong at line 4, what's line 4 tho
Jochem
Jochem10mo ago
that you can't use the null safe property accessor (?.) on the left side of an assignment operator (=)
laith
laith10mo ago
document.getElementById("button")?.onclick = function(){ this so i remove the "="? nvm am dum
Bardacoon
Bardacoon10mo ago
no, remove the ?, listen to @Jochem (i think)
laith
laith10mo ago
dam
Jochem
Jochem10mo ago
remove the ? yeah
laith
laith10mo ago
now it works smh
Jochem
Jochem10mo ago
which script tag did you remove?
laith
laith10mo ago
wdym? the one in the top
4ngel_._
4ngel_._10mo ago
? is optional chaining
Bardacoon
Bardacoon10mo 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
laith10mo ago
all i did was ading then removing the "?" then it works like how
Jochem
Jochem10mo 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
laith10mo ago
ohhhhhhhh i always put my script upper thats why so every thing was bc of the script tag
curator
curator10mo ago
adding a safe check will resolve i guess
Jochem
Jochem10mo ago
not everything, the other things Bardacoon pointed out were still also good points
laith
laith10mo ago
now i can contenue js after 2 weeks of stucking at this error yay
Bardacoon
Bardacoon10mo 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
laith10mo ago
mhm thx every one oh that will help alot now i gtg sleep thx and bye👋
snxxwyy
snxxwyy10mo ago
i'd recommend mdn not w3schools, w3schools has a lot of bad practices and outdated methods.