0

I tried console.log() to see if zoomName has a value, and it does. However, I was trying to see if setAttribute() worked in the function by console logging the id of the div I just created and it said it's unidentified. How do I set attribute in this case? Thanks!

var zoomNum = 0;
function appendImg(imgName){
    zoomNum+=1;
    var zoomName = zoomNum.toString();          //create id name for new zoom class
    zoomName = "zoomId" + zoomName;

    var newZoomClass = document.createElement("div");         //create new zoom class for each image
    newZoomClass.setAttribute("class", "zoom");
    newZoomClass.setAttribute("id", zoomName);            //new ID

    document.getElementsByClassName("infoContent")[0].appendChild(newZoomClass);  //append class to infoContent

    var linkk = document.getElementsByClassName("zoom");
    var theValue = linkk.getAttribute("id");
    console.log(theValue);
Karen C
  • 57
  • 1
  • 7
  • 1
    `val`? Did you mean `var`? – Phil Aug 14 '20 at 01:03
  • 1
    You never added the element to the DOM, how do you expect `getElementsByClassName()` to find it? – Barmar Aug 14 '20 at 01:03
  • Also, `getElementsByClassName` returns a collection, not a single element. – Barmar Aug 14 '20 at 01:04
  • typo at declaring linkk? – Karl L Aug 14 '20 at 01:04
  • 1
    @Phil That must be a copying error or the code wouldn't even run. – Barmar Aug 14 '20 at 01:05
  • @Barmar too many times I ask if there's any errors in the console and they say no. Then later they say something like _"oh, except for these errors but I thought they meant nothing"_ – Phil Aug 14 '20 at 01:06
  • @Phil He says he's getting undefined. If the script can't even be parsed he wouldn't get that. – Barmar Aug 14 '20 at 01:09
  • @Barmar they say they're getting _"unidentified"_ so who knows what to believe. Anyway, I'm sure you're right and it's just a typo here in the question – Phil Aug 14 '20 at 01:10
  • @phil oh sorry bout that, I changed val to var, and moved the console.log after I appended the div to a class, check my edited question – Karen C Aug 14 '20 at 01:16
  • @KarenC in that case, simply see the linked duplicate post. `getElementsByClassName()` does not return what you expect – Phil Aug 14 '20 at 01:20
  • @Phil actually just wanted to check if the attributes were created. In this case, they should have been created right? – Karen C Aug 14 '20 at 01:25
  • Yep, there shouldn't be any problems with the attributes that you set – Phil Aug 14 '20 at 01:29
  • Oh okay thank you! – Karen C Aug 14 '20 at 01:34

0 Answers0