+ 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.
6 Answers
+ 4
rblx _pro here is a quick example
https://sololearn.com/compiler-playground/W95aNEUBVETi/?ref=app
+ 5
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.
+ 3
BroFar so basicaly it is a thing? getElememtByClassName
+ 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
0
thomas642daniel
I think the class in className is referring to css class, not javascript class objects...