venkatachalam
venkatachalam

Reputation: 102439

Hyperlinking for image

I am using JavaScript to specify the target link. It is not working properly. What change should I do in order for it to work correctly?

My code:

var link = create_element(cell, 'img');
link.setAttribute("src", "images/sub.png"); 
link.href = "http://localhost/";

Upvotes: 1

Views: 233

Answers (4)

Real Red.
Real Red.

Reputation: 5039

Yes, exactly. We can also write:

link.onclick = function() { document.location.href = "http://localhost"; };

Upvotes: 1

NIConRoaming
NIConRoaming

Reputation:

You can't use href for an img tag. What about adding a clickhandler?

link.onclick = function(){top.location.replace("http://localhost");};

Upvotes: 5

Robin Day
Robin Day

Reputation: 102468

As Sergei stated, you need a link with an image inside it. Below is an extension to your code. It's completely untested though but gives a general idea on how you might achieve it based on what you had already.

var link = create_element(cell, 'a');
link.href = "http://localhost/";
var image = create_element(link, 'img');
image.setAttribute("src", "images/sub.png");

Upvotes: 5

Sergei
Sergei

Reputation: 2757

IMG tag doesn't have href attribute. "A" tag has it. So you should create A with desired HREF, then IMG inside.

Upvotes: 4

Related Questions