Why cant i get thiz to work? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why cant i get thiz to work?

It works when i use the ById, but not ClassName or TagName <!DOCTYPE html> <html> <head> <title>Page Title</title> <style type="text/css"> body { text-align: center; } p { border: 2px solid; border-radius: 25px; } </style> </head> <body> <h2 id="questOne">This is the Question. hope you're ready!!</h2> <p class="yu" id="ansa" onclick="funcA()">Some possible answer here!</p> <p class="yu" id="ansb" onclick="funcB()">Some possible answer here!</p> <p class="yu" id="ansc"onclick="funcC()">Some possible answer here!</p> <p class="yu" id="ansd"onclick="funcD()">Some possible answer here!</p> <script type="text/javascript"> window.onload = function() { var samp = document.getElementsByClassName("yu"); samp.innerHTML = "Why you not working???"; } </script> </body>

2nd May 2019, 4:08 PM
Cyrus The Virus!
Cyrus The Virus! - avatar
13 Answers
0
Black Frost They say that it is considered poor practice to use "onclick" in HTML and that it is better to use eventListeners in the script. Here is how you could change the background color doing it that way: <body> <h2 id="questOne">This is the Question. hope you're ready!!</h2> <p class="yu" id="ansa">Some possible answer here!</p> <script> window.onload = function() { ansa.addEventListener('click',funcA); function funcA(){ var A = document.getElementById('ansa'); A.style.backgroundColor="red"; } } </script> </body>
2nd May 2019, 6:18 PM
Decimis † 𝕯𝖊𝖈𝖎𝖒𝖎𝖘
Decimis † 𝕯𝖊𝖈𝖎𝖒𝖎𝖘 - avatar
+ 3
Nothing happens the text remains the same as written in html
2nd May 2019, 4:19 PM
Cyrus The Virus!
Cyrus The Virus! - avatar
+ 3
Thanks i just realized that they are stored as an array, need for loop
2nd May 2019, 4:23 PM
Cyrus The Virus!
Cyrus The Virus! - avatar
+ 2
Oh, lol 😅 I just left it there because he had it there already. Personally I always use external JS rather than clogging up the HTML.
3rd May 2019, 12:57 AM
Decimis † 𝕯𝖊𝖈𝖎𝖒𝖎𝖘
Decimis † 𝕯𝖊𝖈𝖎𝖒𝖎𝖘 - avatar
+ 1
What's your problems exactly
2nd May 2019, 4:17 PM
Basel.Al_hajeri?.MBH()
Basel.Al_hajeri?.MBH() - avatar
+ 1
What function changes bg of button onclick?
2nd May 2019, 4:37 PM
Cyrus The Virus!
Cyrus The Virus! - avatar
+ 1
So you dont use () inside an eventlistener? Thats probably where i messed up
2nd May 2019, 6:28 PM
Cyrus The Virus!
Cyrus The Virus! - avatar
+ 1
Black Frost No () after funcA in the line: ansa.addEventListener('click', funcA) for funcA if you that is where you meant.
2nd May 2019, 6:50 PM
Decimis † 𝕯𝖊𝖈𝖎𝖒𝖎𝖘
Decimis † 𝕯𝖊𝖈𝖎𝖒𝖎𝖘 - avatar
+ 1
Because getElementsByClassName() is different to getElementById() Console log your variable after getElementsByClassName(), you see HTMLCollection type You need to use [0] to get the first in this array-like object, and then .innerHTML property. A hint is whether in the function there is 's' following 'Element'
3rd May 2019, 12:51 AM
Gordon
Gordon - avatar
+ 1
Decimis † 𝕯𝖊𝖈𝖎𝖒𝖎𝖘 You can omit window.onload as your script is at end of body and DOM is created already. https://code.sololearn.com/WYNhzklIhJEF/?ref=app
3rd May 2019, 12:55 AM
Gordon
Gordon - avatar
+ 1
https://code.sololearn.com/WGAjaZ553jMG/?ref=app
3rd May 2019, 2:12 AM
Gordon
Gordon - avatar
0
Great to understand that alone
2nd May 2019, 4:36 PM
Basel.Al_hajeri?.MBH()
Basel.Al_hajeri?.MBH() - avatar
0
Also it's easier to help if you have the actual code linked here rather than the text copy & pasted in. That way, if anyone needs to modify and run the code to test an answer, they don't need to start a code themselves then copy & paste your text into that, they can just click on your code to play around with it 😉
2nd May 2019, 6:25 PM
Decimis † 𝕯𝖊𝖈𝖎𝖒𝖎𝖘
Decimis † 𝕯𝖊𝖈𝖎𝖒𝖎𝖘 - avatar