Onclick setAttribute href of src

I am working on a project to replace thumbnails with a larger image when the thumbnail is selected and it works fine, but now I would like end users to be able to click on the larger image to display it in jquery.lightbox-0.5. The problem is that after selecting a thumbnail, the uploaded image does not have href. I was hoping I could pass src images to href using the onclick event, but I cannot figure out how to do this.

Here is my code

<script type="text/javascript">
function swaphref(image) {

document.getElementById('image').setAttribute('href', this.src);
//window.alert(document.getElementById('image').src);
}
</script>

<body>
<div class="productimage" id="gallery"><a onclick="swaphref(this)"><img id="image" img src="product_images/<?php echo $targetID; ?>/<?php echo $targetID; ?>.jpg" width="253" height="378" border="0" /></a></div>

</body>
+3
source share
4 answers

Ok I got this job. "A" is missing an identifier.

Here is the code .....

<script type="text/javascript">
function swaphref(imagehref) {

document.getElementById('imagehref').setAttribute('href', image.src);

//window.alert(document.getElementById('image').src);
//window.alert(document.getElementById('imagehref').href);
}
</script>

<body>
<div class="productimage" id="gallery"><a href="#" id="imagehref"     onclick="swaphref(this)"><img id="image" img src="product_images/<?php echo $targetID; ?  >/<?php echo $targetID; ?>.jpg" width="253" height="378" border="0" /></a></div>

</body>

Thanks to everyone who tried to help.

+3
source

, href src. , :

document.getElementById(''). setAttribute ('href', this.src);  //window.alert(document.getElementById('image ') SRC);.

href , src, src , href. , window.alert

:

document.getElementById(''). setAttribute ('src', this.href ');  window.alert(document.getElementById( ''). SRC

.

0
var image = document.getElementById('image');
image.setAttribute('href', image.src);

Must work.

0
source

change this line

document.getElementById('image').setAttribute('href', this.src); 

in this way

document.getElementById('image').setAttribute('src', this.href); 
0
source

All Articles