-1

I need to add links to some divs in Wordpress, so I tried writting some JS code for it. But I keep getting the following error " Uncaught TypeError: addId.setAttribute is not a function "

I'll leave the code down bellow. Thank you everyone for the help

var addId = document.getElementsByClassName("elementor-repeater-item-d7362c2");

addId.setAttribute('id', "item");


ogElement = document.getElementByid("item").innerHTML;
addLink = "<a href='https://www.vivendus.com.br/product-category/colar/'>" + ogElement + "</a>";
document.getElementByid("test").innerHTML = addLink;
.elementor-repeater-item-d7362c2 {
  color:#ccff00;
  font-size:2em;
}
<div class ='elementor-repeater-item-d7362c2'> test <div>
  • 1
    Does this answer your question? [What do querySelectorAll and getElementsBy\* methods return?](https://stackoverflow.com/questions/10693845/what-do-queryselectorall-and-getelementsby-methods-return) – Anurag Srivastava Jun 22 '21 at 17:49

2 Answers2

0

document.getElementsByClassName returns a collection of elements, not an element. You should specify which element you are working with.

Clément Cartier
  • 174
  • 2
  • 11
0
var addId = document.getElementsByClassName("elementor-repeater-item-d7362c2");

This will return an array or a list if i remember right, you need to get it by id or iterate on addId and setAttribute on each element !

nerap
  • 226
  • 1
  • 2
  • 15