javascript acces collections
snippet in html

javascript acces collections

user737

<!-- This is HTML -->
<p>Hello World!, and I'm at Index One</p>
<p>Hello Norway!</p>
<p id="demo">Try it out!</p>

<script>
// This is JavaScript
var myCollection = document.getElementsByTagName("p");
document.getElementById("demo").innerHTML =
"The innerHTML of the second paragraph is: " +
myCollection[1].innerHTML;
  
// or
console.log(myCollection[2]);
</script>

javascript access collections of elements

user8430

<div class="wrapper">
  <p class="box">Food</p>		<!-- Index Position 0 -->
  <p class="box">Drink</p>		<!-- Index Position 1 -->
  <p class="box">Books</p>		<!-- Index Position 2 -->
  <p class="box">Whatever</p>	<!-- Index Position 3 -->
</div>

<script>
var myCollection = document.getElementsByClassName("box");
console.log(myCollection)
myCollection[0].innerHTML = "I can change everything!"
</script>