0
Skipping a tag in JS
Does anyone know how you would skip a tag? For instance <p>Hello!</p> <p></p> <p></p> <p></p> I have some JS that is going to output 8 calculations to the p tags but I dont want the first p tag to be changed. I will have 9 p tags but the last 8 need change? JS is document.getElementsByTagName("p")[i].innerHTML = planetAge + " years old.";
12 Answers
+ 4
Angelina Ahh that's what was required, link to the full code. Use '<=' (less than equal to) instead of < (less than) ;)
for (i = 1; i <= planets.length; i++)
and rest of the code as is!
Edit: Because the index now starts from 1, so <= is mandatory to change the tag for every planet.
+ 4
Angelina, use this and try again:
for (i = 1; i < planets.length; i++) {
var planetAge = age / planets[i - 1].orbit;
document.getElementsByTagName("p")[i].innerHTML = planetAge + " years old.";
}
+ 2
Are you using a for loop? You can just start i at 1 instead of 0
+ 1
Use this:
var array = document.getElementsByTagName(âpâ;
for(int i=1; i < array.length;i++){
array[i].innerHTML = text;
}
It wonât use the first, but will get the last
+ 1
Doing the above doesn't calculate the correct ages :/
+ 1
If it helps I have added code to my profile, also you can find it in codepen.
https://codepen.io/angelinalblyth/pen/rdgbJd
+ 1
Thank you so much! Didnt even think of that
0
@Ariela
If I do that then my last calculation is missed out as it has reached my .length by the time it gets to the last <p>
0
Thats changing the first p tag again. This is the code I currently have and it works fine if I dont have the first p tag in my HTML.
for (i = 0; i < planets.length; i++) {
var planetAge = age / planets[i].orbit;
document.getElementsByTagName("p")[i].innerHTML = planetAge + " years old.";
}
0
I didn't even see Rahul's solution, I have tried it and it still missing the last P tag and isn't replacing the text already in the P tag.
0
does anyone know how to open a link in specific area of a webpage?