+ 1

getElementByClass ?

In JavaScript I have this from a lesson: let userName = prompt(ā€œPlease enter your nameā€); document.getElementById(ā€˜userNameā€™).textContent = userName; And this is the html: <p>Hello </p><p id = userName></p> i wanna know if i can change everything that has to do with id to class like ā€œdocument.getElememtByClassā€ and ā€œ<p class = userName> so that I can call it whenever I want since I canā€™t just make multiple name ids since thatā€™s not allowed (btw I donā€™t even know if any of this is possible if so pls tell me.

11th Sep 2024, 9:42 PM
rblx _pro
rblx _pro - avatar
6 Answers
11th Sep 2024, 10:12 PM
BroFar
BroFar - avatar
+ 4
Yes, in JavaScript, classes are objects. This means they have properties and methods, just like any other object. The id property, however, is not a built-in property of classes in JavaScript. It's more commonly used to identify HTML elements in the DOM.
11th Sep 2024, 10:53 PM
BroFar
BroFar - avatar
+ 3
BroFar so basicaly it is a thing? getElememtByClassName
11th Sep 2024, 10:52 PM
rblx _pro
rblx _pro - avatar
+ 3
Remember that classes might be used by multiple elements. so it's plural form. 's' here v document.getElementsByClassName('yourclass'); and it returns an HTMLCollection, not a single element. https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName
12th Sep 2024, 9:42 AM
Bob_Li
Bob_Li - avatar
0
In JavaScript, classes are indeed objects, which means they possess properties and methods like any other object. However, the id property is not a standard feature of JavaScript classes. It is typically used to identify HTML elements within the DOM.
13th Sep 2024, 10:57 AM
thomas642daniel
0
thomas642daniel I think the class in className is referring to css class, not javascript class objects...
13th Sep 2024, 1:01 PM
Bob_Li
Bob_Li - avatar