What's the difference between querySelector and getByClassName ?How can I console out my Sprite in JavaScript? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What's the difference between querySelector and getByClassName ?How can I console out my Sprite in JavaScript?

What is wrong, I try to print out SpritePlease help with JavaScript code, I'm stuck here in my document.selectByClassName https://code.sololearn.com/WSMG536eZ0Cg/?ref=app

7th Apr 2020, 2:58 PM
kaspars kaspars
kaspars kaspars - avatar
32 Answers
0
replace : function alertHello = alert(player); with: alert(player); and name has to be a string (vasya) without " ", means it is a variable. so : var name = "vasya" ;
7th Apr 2020, 5:59 PM
Bahhaⵣ
Bahhaⵣ - avatar
+ 2
you don't always have to use window.onload. you could put your javascript at the end of html. there are many scenarios.
7th Apr 2020, 4:55 PM
Bahhaⵣ
Bahhaⵣ - avatar
+ 1
add onload function to your code. window.onload =function() { //your entire code here } document. getElementsByClassName() add s to "Element"
7th Apr 2020, 3:15 PM
Bahhaⵣ
Bahhaⵣ - avatar
+ 1
window.onload=function(){ var drink = document.getElementsByClassName("cold"); drink console.log(drink[1].textContent); }
7th Apr 2020, 3:18 PM
Bahhaⵣ
Bahhaⵣ - avatar
+ 1
What is window.onload function, what does it do, when and how it's used? And what is used for document.querySelector and what for is document.getElementBy Id/className/TagName in 7 hours can't figure out till the end
7th Apr 2020, 3:24 PM
kaspars kaspars
kaspars kaspars - avatar
+ 1
yes everything inside. otherwise the code will execute before the page is loaded and it will not see your html.
7th Apr 2020, 3:41 PM
Bahhaⵣ
Bahhaⵣ - avatar
+ 1
window.onload not windows remove s.
7th Apr 2020, 3:42 PM
Bahhaⵣ
Bahhaⵣ - avatar
+ 1
since now you're using queryselector. use CSS selector (".cold") not ("cold" ), just like what would you write in css code. window.onload = function() { var drink = document.querySelectorAll(".cold") console.log(drink[1].textContent) }
7th Apr 2020, 3:49 PM
Bahhaⵣ
Bahhaⵣ - avatar
+ 1
select first then console.log() and add } at the end. just try the code above your last reply.
7th Apr 2020, 3:53 PM
Bahhaⵣ
Bahhaⵣ - avatar
7th Apr 2020, 3:54 PM
kaspars kaspars
kaspars kaspars - avatar
+ 1
var name = "vasya" var level = 25 var player = "your name is " + name + "and you are " + level + " level" console.log(player) function sayHello(name) { alert("Hi, " + name); } sayHello("David"); function door(level) { if (level < 10) { alert("return after " + bcd + " levels");} else if(level >= 10) { alert("you can pass"); } } bcd = 10 - level door(7); door(11);
7th Apr 2020, 5:19 PM
Bahhaⵣ
Bahhaⵣ - avatar
+ 1
you keep using variables before they are created. the order of lines matters. and concatenation with + does not need comma (,)
7th Apr 2020, 5:21 PM
Bahhaⵣ
Bahhaⵣ - avatar
+ 1
it works fine, it's printing the else statement because you used door(11) after door(7)
7th Apr 2020, 7:04 PM
Bahhaⵣ
Bahhaⵣ - avatar
+ 1
Question not found kattar, and i wake up and made new problems to fix, why my JavaScript ain't working now? I have loaded windows onload, I have selected by getElementByClass name I have set all variables but it ain't printing what I need... how do I place the 3 properties the right way inside small-crate? window.onload = function() { var armor = "leather" var sword = "metal" var gold = 150 var smallCrate = document.getElementByClassName("small-crate") smallCrate = gold, sword, armor alert(smallCrate) https://code.sololearn.com/WO8zAfu0E633/?ref=app
8th Apr 2020, 8:14 AM
kaspars kaspars
kaspars kaspars - avatar
+ 1
getElementsByClassName Elements not Element told you already yesterday.
8th Apr 2020, 8:54 AM
Bahhaⵣ
Bahhaⵣ - avatar
+ 1
var drink = document.getElementsByClassName("cold"); Array.from(drink).forEach( function(cold){ drink.textContent += "drinkii" }) you did not wrap the whole function inside foreach parentheses. you have a dot . after "drinkii" you forgot to write "document" document.getElementsByClassName
8th Apr 2020, 11:41 AM
Bahhaⵣ
Bahhaⵣ - avatar
0
+ window.onload=function () {} means execute the javascript code when the entire page is loaded. without it in your case, javascript execute before the html is loaded so it doesn't see any of your class id... and it returns undefined.
7th Apr 2020, 3:36 PM
Bahhaⵣ
Bahhaⵣ - avatar
0
You mean I have to put windows.onload { And absolutely everything from JavaScript here??? } still don't work with this onload I don't understand https://code.sololearn.com/WSMG536eZ0Cg/?ref=app
7th Apr 2020, 3:39 PM
kaspars kaspars
kaspars kaspars - avatar
7th Apr 2020, 3:45 PM
kaspars kaspars
kaspars kaspars - avatar