+ 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.
5 RĂ©ponses
+ 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...