error

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