T
T
TheTecnika2022-04-14 23:54:27
JavaScript
TheTecnika, 2022-04-14 23:54:27

How to replace html code via Javascript?

how to shorten links in html code with JS?
For example, there is such a piece

<a href="http://site.site/uploads/posts/2009-01/1.gif" ><img src="/uploads/posts/2009-01/thumbs/1.gif" tooltip="Описание"></a>

and turn it into
<a href="/1.gif" ><img src="/1.gif" tooltip="Описание"></a>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vova, 2022-04-15
@vovvkka

I don't know what it makes sense, but if it's necessary, then:

<a id="site-link"> <img tooltip="Описание" id="site-photo"></a>

document.querySelector('#site-link').setAttribute('href', 'http://site.site/uploads/posts/2009-01/1.gif');
document.querySelector('#site-photo').setAttribute('src', '/uploads/posts/2009-01/thumbs/1.gif');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question