error

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

Did you find this page helpful?