The document.getElementByClassName method

how do you get the method to work when your js is inside your html file???

the method works totally fine even in that case - maybe you need to show the code you are having issues with?

ok
say something like this

script{
  var v = document.getElementsByClassName("Ep");
                v.style.color = "66FF07";
                v.style.height = "150px";
}

that doesn’t seem valid HTML nor JS - is that what you have written in the file?

ok
so how can I fix it

write a correct script element and put in there correct js code

to use script element in main html is using
<script>
//your code goes here
</script>
not script {
}

and to use script with file.js is using <script src=“yourfile.js”></script>

ahh 1 more. document.getElementsByClassName need indexes to get the specific elements. cuz getElementsByClassName output is HTMLCollections (An array). assume there has 4 elements that have class=“myElements”. so if u want to styling the element 2 with javascript u need to use indexes to get the elements.
var v = document.getElementsByClassName(“myElements”)[1] (indexes starting in 0)
v.style.color = “red”;
v.style.height = “150px”;

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.