#What does this mean in javascript
1 messages · Page 1 of 1 (latest)
<@&987246964494204979> please have a look, thanks.
the website html has tags, remember?
for example a website might look like:
<html>
<h1>Hello</h1>
<p>huhu</p>
</html>
now, that javascript snippet will give u all objects of the website with a tag of h1
so in this case, the <h1>Hello</h1> thing there
I understand, but what is its point?
u could for example manipulate it
remove it
color it red
change the text
whatever u want
thats how websites work
WHat would be an example code of that
const objects = document.getElementsByTagName("h1");
const firstHeading = objects[0]; // we only want the first, if multiple
firstHeading.innerHTML = "aaaaa";
that would change its text from Hello to aaaa
But where is the paragraph text like <p> hdurhfr </p>
What does the objects(0) mean
that getElementsByTagName method returns u an array
array of what?
which is a container, like a list
Ohhh
of all objects with tag h1
cause the website could containb multiple
then, objects[some number] will return u the object in that array at that "position"
(index)
indices start counting at 0
I get what you are saying now
so the first element is objects[0]
But for what ur saying rn im sorta lostt....
if ud want to for example color all h1 headings in the entire webpage with a red background, ud loop over it:
Can you forst define array?
How would I loop it?
Im really dumb, sorry
ur not dumb
ur just asking questions anyone has in their first weeks
const headings = document.getElementsByTagName("h1");
for (let i = 0; i < headings.length; i++) {
const heading = headings[i];
heading.style.backgroundColor = "red";
}
ur probably better off following a structured guide
a book, a video, a tutorial
Ok
cause otherwise u just run from one question into the next
and everything is confusing
Yeah thats true
🙂
its a loop
for loops?
it reads like "do the following for each ..."
So it loops the code?
for (let i = 0; i < 10; i++) {
...
}
Thanks for the help, much appreciated!
i++ meaning?