allstar
allstar

Reputation: 29

How to change image's href with Javascript by using title name?

Here in this slideshow there are around 50 images. I use a platform which is similar to wordpress and doesnt let me to change image href. The images have the same class name. Is it possible to use title name(which is unique for each image) and change some image's href?

<div id="slideshow" class="vlt-d-slider" style="position: relative; overflow: hidden; width: 928px; height: 522px;">
    <div class="slide" data-vltcaption="0" data-brand="none" data-topic="INST" data-url="_0.jpg" style="position: absolute; left: 0px; visibility: visible; z-index: 3; line-height: 520px; height: 522px;">
<a class="slide-lightbox" rel="vlt-slideshow" href="m_0.jpg" title="Pfusc number photo 1" target="_blank">
<img class="slide-img" src="_0.jpg" alt="Pfusc number photo 1">
</a>
</div>
      <div class="slide" data-vltcaption="1" data-brand="none" data-topic="INST" data-url="0.jpg" style="position: absolute; left: 0px; visibility: hidden; z-index: 1; line-height: 520px; display: block; height: 522px;">
<a class="slide-lightbox" rel="vlt-slideshow" href="f_0.jpg" title="Pfusc number photo 2" target="_blank">
<img class="slide-img" src="0.jpg" alt="Pfusc number photo 2">
</a>
</div>
      <div class="slide" ....
</div>
    </div>

Upvotes: 0

Views: 197

Answers (2)

sergonius
sergonius

Reputation: 21

Firstly, images don't have href, they have src.

Secondly, I think you mean alt when you say "title name"?

In that case, document.querySelector('[alt="the title"]').src = 'the new src';

If you want to find it by the title of the link, you would have to do document.querySelector('a[title="the title"] > img').src = 'the new src';

Upvotes: 2

Zvezdas1989
Zvezdas1989

Reputation: 1465

For start I'm not sure do you want to change your a tag or img tag properties, but I will presume it's a tag because of you mentioned href. Yes it is. You can do it like this:

$("a[title='yourTitle']").attr('href', 'https://www.google.com/');

and if you want image:

$('img[alt="Your Alt"]').attr('src', 'image.jpg');

Upvotes: 0

Related Questions