-2

I have this HTML:

<div class="myclass" id="myid">

Trying to use the following to search for the element with this ID inside this class:

var getDiv = document.getElementById("myid").getElementsByClassName("myclass")[0];
getDiv.setAttribute("style", "visibility: hidden");

This returns getDiv as 'undefined'

I will have numerous elements with the same class or ID, but only one element exists on the page with both.

jmbronson
  • 13
  • 1
  • 3

1 Answers1

0

What you are trying to do is invalid. Id is only used for one element, and class is used for multiple. And you can only do this:

var something = document.getElementById("myId");
something.setAttribute(...);

or

var something = document.getElementsByClassName("myClass"); //this is now array of elements
something[0].setAttribute(...); // I chose first element

I hope this helps. To find out more about id vs class click here.

melpomene
  • 84,125
  • 8
  • 85
  • 148
Danilo Ivanovic
  • 1,216
  • 1
  • 10
  • 18