How to get and use all the <p> tag element in HTML page in JavaScript

2 Answers

0 votes
<!DOCTYPE html>
<html>
  
<head></head>
  
<body>

<p id="p1-id">p1-id tag</p>
<p id="p2-id">p2-id tag</p>
<p id="p3-id">p3-id tag</p>

<script>
 
var  all_p = document.getElementsByTagName("p");
var i;
for (i = 0; i < all_p.length; i++) 
    document.write(all_p[i].innerHTML + " ::: ");
 

/*
run:
  
p1-id tag ::: p2-id tag ::: p3-id tag ::: 
  
*/

</script>


</body>
  
</html>

 



answered Jul 12, 2015 by avibootz
0 votes
<!DOCTYPE html>
<html>
  
<head></head>
  
<body>

<p id="p1-id">p1-id tag</p>
<p id="p2-id">p2-id tag</p>
<p id="p3-id">p3-id tag</p>

<script>
 
var  all_p = document.getElementsByTagName("p");
var i;
for (i = 0; i < all_p.length; i++) 
    all_p[i].style.backgroundColor = "peachpuff";
 

/*
run:
  
the backgroundColor of all <p> tags will change to "peachpuff"
  
*/

</script>


</body>
  
</html>

 



answered Jul 12, 2015 by avibootz

Related questions

1 answer 209 views
1 answer 245 views
2 answers 254 views
254 views asked Dec 1, 2018 by avibootz
3 answers 396 views
...