Anon Omus
Anon Omus

Reputation: 381

How to make an image object a link with Javascript

HTML equivalent:

<a href='#'> <img src='...'....../></a>

Looking for something along the lines of:

var im = document.createElement("img");
im.ADDLINK;

Relevant code:

var close = document.createElement("img");
    close.src="close.png";
    close.width = 50;
    close.height = 50;
    close.border = 1;
    close.className="closeButton";
    close.style.cssFloat="right";

    var link = document.createElement("a");
    link.href = "#";

Make close image a link:

    link.appendChild(close);

    close.style.right=0+"px";
            //Div img is a div created above this code snippet:
    divimg.appendChild(close);

Upvotes: 0

Views: 2818

Answers (2)

Christopher D&#237;az
Christopher D&#237;az

Reputation: 346

You can try this

<script>
   function imgRedirect(destination){

     location.href = destination;

   }
</script>
<img src="..." onclick="imgRedirect('http://google.com')"/>

Upvotes: 0

tymeJV
tymeJV

Reputation: 104775

Create the link:

var link = document.createElement(a);
link.href = "http://...com";

Add the image:

var im = document.createElement("img");
link.appendChild(im);

And append it to the DOM where needed

Upvotes: 3

Related Questions