+ 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 odpowiedzi
11th Sep 2024, 10:12 PM
BroFar
BroFar - avatar
+ 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.
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
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