Replace href value using javascript

I have the following code which changes the big image according to the thumbnail clicked (this part is working).

I want to change accordingly the url in the href (so that each big image shown has a link to another URL). <-- this last part is not working.

----------HERE IS THE CODE ----------------------






 



image swapping 








Replace href value using javascript
Replace href value using javascript
Replace href value using javascript
Replace href value using javascript
Replace href value using javascript
Replace href value using javascript
Replace href value using javascript

asked Jan 19, 2014 at 22:06

1

This question might get flagged as a duplicate, but here's the answer anyway.

To change the href value of an anchor:

document.getElementById('file').setAttribute('href',url);

answered Jan 19, 2014 at 22:08

Replace href value using javascript

SchienSchien

3,7171 gold badge15 silver badges29 bronze badges

7

You need to change logic slightly...Thumbnails doesn't have links, right?

    

Html:

 
Replace href value using javascript
Replace href value using javascript
Replace href value using javascript

answered Jan 19, 2014 at 22:42

Replace href value using javascript

sinisakesinisake

11k2 gold badges18 silver badges27 bronze badges

2

I ran the code in Firebug, and found the error:

   th=document.getElementById('thumbs').getElementsByTagName('img');
for(c=0;c

"this" refers to th[c], and now look at your image tags:

 
Replace href value using javascript
Replace href value using javascript
Replace href value using javascript

They don't have href attribute, so your swapFile function fails and terminates script execution. You could add custom attributes to these images:

 
Replace href value using javascript
Replace href value using javascript
Replace href value using javascript

But images are not anchors, so th[c].href will not give you the value.

Here's the new init function:

function init(){

   th=document.getElementById('thumbs').getElementsByTagName('img');

for(c=0;c

I have tested the code locally and it runs.

answered Jan 19, 2014 at 22:52

Replace href value using javascript

SchienSchien

3,7171 gold badge15 silver badges29 bronze badges





 


answered Nov 13, 2021 at 7:21

Anchor href Property.
Change the destination (URL) of a link: getElementById("myAnchor"). href = "http://www.cnn.com/";.
Get the URL of a link: getElementById("myAnchor"). href;.
Another example of how to get the URL of a link (a relative URL): var x = document. getElementById("myAnchor"). href;.

How do I change a href value?

Answer: Use the jQuery . attr() Method attr() method to dynamically set or change the value of href attribute of a link or anchor tag. This method can also be used to get the value of any attribute.

How to add href with jQuery?

jQuery: Set href attribute at runtime Set href attribute at runtime using jquery. JavaScript Code: $(document). ready(function(){ $('#button1').

How do you make a dynamic href in HTML?