-1
  1. i am currently trying to add a class to a html element that is imported by php.The php file contains a a tag element
<a class="felt" href="felt.php">Felt Products</a>

i can not see the class being added, why is this? can js not effect imported elements?

<?php include 'headtaglinks.html'; ?>  
<script>
 var element = document.getElementsByClassName("felt");
 element.classList.add("my-class");
</script>
  1. if i am being able to add a class to a php imported html element, will the added class stay when clicking on a href link? or will the class stay and be written unless i remove it?
Barmar
  • 741,623
  • 53
  • 500
  • 612
KAZ
  • 11
  • 1
  • 2

1 Answers1

0

You're using getElementsByClassName which returns an array.

You need to modify your code to access the first element of the array or use another selector function like querySelector('.felt')

<script>
 var element = document.getElementsByClassName("felt")[0];
 element.classList.add("my-class");
</script>
<script>
 var element = document.querySelector(".felt");
 element.classList.add("my-class");
</script>
cam
  • 3,179
  • 1
  • 12
  • 15