views:

119

answers:

4

Dear all, 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/";

Kindly help me

+4  A: 

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

Sergei
+5  A: 

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");
Robin Day
+5  A: 

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

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

yes exactly, we can also write

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