+ 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 odpowiedzi
+ 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...