#What does this mean in javascript

1 messages · Page 1 of 1 (latest)

odd copper
lapis shoalBOT
#

<@&987246964494204979> please have a look, thanks.

glossy raft
#

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

odd copper
#

I understand, but what is its point?

glossy raft
#

u could for example manipulate it

#

remove it

#

color it red

#

change the text

#

whatever u want

#

thats how websites work

odd copper
#

WHat would be an example code of that

glossy raft
#
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

odd copper
#

But where is the paragraph text like <p> hdurhfr </p>

glossy raft
#

i dont quite follow

#

u have the html file and the javascript runs on this file

odd copper
#

What does the objects(0) mean

glossy raft
#

that getElementsByTagName method returns u an array

odd copper
#

array of what?

glossy raft
#

which is a container, like a list

odd copper
#

Ohhh

glossy raft
#

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

odd copper
#

I get what you are saying now

glossy raft
#

so the first element is objects[0]

odd copper
#

But for what ur saying rn im sorta lostt....

glossy raft
#

if ud want to for example color all h1 headings in the entire webpage with a red background, ud loop over it:

odd copper
#

Can you forst define array?

odd copper
#

Im really dumb, sorry

glossy raft
#

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

odd copper
#

Ok

glossy raft
#

cause otherwise u just run from one question into the next

#

and everything is confusing

odd copper
#

Yeah thats true

glossy raft
#

🙂

odd copper
#

Ok, for my final question cuz i gtg, what is

glossy raft
#

its a loop

rotund needle
#

for loops?

glossy raft
#

it reads like "do the following for each ..."

odd copper
#

So it loops the code?

glossy raft
#

yeah, it repeats the contained stuff

#

and that i stuff controls how often etc

odd copper
#

how would you set it to like repeat this for x times

#

Lol ok

glossy raft
#
for (let i = 0; i < 10; i++) {
  ...
}
odd copper
#

Thanks for the help, much appreciated!

glossy raft
#

or alternatively with a while-loop:

#
let i = 0;
while (i < 10) {
  ...
  i++;
}
odd copper
#

i++ meaning?

glossy raft
#

i = i + 1;

#

increment by one

odd copper
#

oh ok

#

Thanks, cya