Change link from two links whose id are equal with js

Asked

Viewed 41 times

-3

I had to duplicate a code from a link whose id is the same (I can’t change it, I can’t actually change it). And I need to change the text of that link. I did it that way:

document.getElementById("send").innerHTML = "Enviar itens";

However, it only changes the text of the first link, and as stated, I need to change the text of the two links.

Link code:

<a id="send" href="rota" class="buy send">Send</a>

<a id="send" href="rota" class="buy send">Send</a>

I had to duplicate because I don’t have access to "pure" html, I call the content through controls from my CMS platform.

  • Add an example of the code and explain why you needed to duplicate these elements.

  • I edited there @Leandroangelo

1 answer

1


document.getElementById() supports only name at a time and returns only a single Node, not an array of nodes. You have several different options:

You can implement your own function that takes multiple ids and returns multiple elements.

You can use Document.querySelectorAll() that allows you to specify multiple ids in a CSS selector string.

You could put common class names on all these nodes and use Document.getElementsByClassName () with a single class name.

  • I got it with querySelectorAll(). Vlw!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.