Skip to content
Sign upLog in
← Back to Community
How to run a class in html and javascript?
Profile icon
MatthewX

How do I run

when a button is pressed?

Answered by Coder100 [earned 5 cycles]
View Answer
Voters
Profile icon
William-BiraneD
Profile icon
MatthewX
Comments
hotnewtop
Profile icon
Coder100

Hi! Classes are not the same as ids. They can be assigned to multiple elements so they can be identified as a group.

Anyways, to get all elements:

document.querySelectorAll('.[class name]').forEach(el => { // now, el is the element you are looking for, and you can do things accordingly el.style.background = "orange"; })
Profile icon
MatthewX

@Coder100
What does this code mean? sorry, but I am still a newbie.

Profile icon
MatthewX

@Coder100
nvm i dont need this i realized the code still works.

Profile icon
MatthewX

@Coder100
How would you add an item to a list in javascript?

Profile icon
Coder100

um, using append

@MatthewX

Profile icon
MatthewX

@Coder100
wait how

Profile icon
MatthewX

@Coder100
Also should i add anything else to the pokemon game? Or what should i improve on?

Profile icon
Coder100
let newItem = document.createElement("li"); let tag = document.getElementById("tag"); tag.appendChild(newItem);
<ol id="tag"></ol>

https://www.w3schools.com/jsref/met_node_appendchild.asp


@MatthewX

Profile icon
Coder100

there ya go

@MatthewX

Profile icon
MatthewX
Profile icon
Coder100
Profile icon
MatthewX

@Coder100
wait so how would i do this to store the pokemon the player has?

Profile icon
MatthewX

@Coder100
um, do you know how to move a character with html and javascript?

Profile icon
MatthewX

@Coder100
how come adding the pokemon to the list says the list is still empty? Can u check in the code?

Profile icon
PattanAhmed

@MatthewX
Hi,
This is an amazing website explaining about Classes (div) in HTML and JavaScript...
Click here

Hope this helps
Please mark my answer if this helps

Profile icon
SixBeeps

Classes in HTML are different than classes in any other language. Here, they are used for grouping together common elements or groups of elements so that they can be styled or identified in bulk.