1

Might be a very simple javascript injection question, but say I have an image html tag:

<a href="myfile.htm"><img src="rainbow.gif"></a> 

I wanted to perform a javascript, such that when clicked on the image, it doesn't go to the myfile.htm. In other words, I wanted to strip the a href which surrounds the img. How can I do this in javascript? Say that I have the following to reference the image tag:

document.elementFromPoint(%f, %f)

f can be replaced by any double/float value

2
  • I am not sure how the second half of your question has anything to do with the first. I have followed up to the point when you asked about stripping the href from the anchor using javascript (which is possible), but then you started talking about some double/float values where I lost your line of thoughts. Commented Jan 15, 2012 at 17:31
  • oh just ignore the double/floats, I am just saying that the way I get the image tag is by using elementFromPoint function, that's all.. ignore the f stuff if that confuses you Commented Jan 15, 2012 at 17:33

5 Answers 5

2

If you have a reference to the img element, then its parent (parentNode) will be the link (in the structure you've given). Three options:

  1. Remove the link entirely
  2. Disable the link
  3. Change the link's href

1. Remove the link entirely

You can remove the link entirely by doing this:

var link       = img.parentNode,
    linkParent = link.parentNode;
linkParent.insertBefore(img, link);
linkParent.removeChild(link);

That uses parentNode to find the parent and grandparent, insertBefore to move the image, and removeChild to remove the link. Note that this assumes the image is the only thing in the link.

2. Disable the link

If you want to keep the link but render it useless, you can do this:

var link = img.parentNode;
if (link.addEventListener) {
    link.addEventListener("click", function(event) {
        event.preventDefault();
    }, false);
}
else if (link.attachEvent) {
    link.attachEvent("onclick", function() {
        return false;
    });
}
else {
    link.onclick = function() {
        return false;
    }
}

3. Change the href of the link:

This is trivial, just set the href property of the link element (which you can get because it's the parent node of the image) to whatever you want:

img.parentNode.href = /* ...something else */;

For instance:

img.parentNode.href = "http://stackoverflow.com";

...would change the link to point to Stack Overflow.

Live example


Some references:

Sign up to request clarification or add additional context in comments.

7 Comments

so var parent here is the <a href> tag? what is gran then?
@adit: Yes, img.parentNode would be the link in the structure you've quoted. gran would be whatever the parent element of the link is. I've changed the names to be a bit clearer. The DOM is a bit convoluted, to remove an element you have to have a reference to its parent. I've added two other options for you, btw, in case you don't actually want to remove the link.
@adit: I think your comment was cut off.
+1 -- I didn't know you could return false from foo.onclick = function() { return false }; and have the default action be prevented. I thought that only worked with dom level zero handlers: <a onclick = "return false"
@AdamRackis: Yeah, I looked into this a while back: blog.niftysnippets.org/2011/11/story-on-return-false.html
|
1
<a id="anchorWithImage" href="myfile.htm"><img src="rainbow.gif"></a> 

Why not grab the anchor, then set its href to nothing:

var a = document.getElementById("anchorWithImage");
a.href = "javascript:void(0)";

Or grab it and set its click event to cancel the default action, which is to browse to the location of its href property

a.onclick = function(e) {
   e.preventDefault();
}

Or do you want to grab all anchors that have an image as their child element, and strip out their href?

jQuery would make this easy, if that's an option for you

$("a").filter(function() {
    return $(this).children("a").length === 1;
}).attr("href", "javascript:void(0)");

or

$("a").filter(function() {
    return $(this).children("a").length === 1;
}).click(function() { return false; });  //returning false from jQuery handlers
                                         //prevents the default action 

EDIT

If you were to have a reference to the image, and wanted to set its parent's anchor's href, you'd grab it with the parentNode property:

var img = document.getElementById("imgId");
var a = img.parentNode;
a.href = "javascript:void(0)";

6 Comments

how do I grab the anchor if I only have a reference to the <img> tag?
is setAttribute a javascript or jquery function?
@adit - it's a pure JavaScript function - not jQuery
setAttribute("href", "#") has the side effect of scrolling to the top of the page. setAttribute("href", "javascript:") is more innocuous.
Hey Adam - Note that HTMLAnchorElement instances have an href property you can set directly, no need to use setAttribute.
|
0

With jQuery you could use something similar to this

$("a").has("img").click(function(e).preventDefaults();});

Basically all this line does is identifies all tags within the document containing an tag disables standard event process

Comments

0

From your comment on another answer, it sounds like you actually want to change/eliminate the target of links at a specific position in the page. You could do something like this:

var el = document.elementFromPoint(10, 10);

while(el) {
    if(el.nodeName.toLowerCase() == 'a')
        el.href = 'javascript:';

    el = el.parentElement;
}

This would loop up from the selected element, identify if the element is an anchor, and set the href to something that does nothing.

Comments

0

you can change the href of a tag on window load itself, so you need not worry when it is clicked.

window.onload = fundtion(){
var imgs = document.getElementsByTagName('img');
 for(var i=0;i<imgs.length;i++){
   var parentElem = imgs[i].parentNode;
   if(parentElem.tagName === 'A') 
   parentElem.setAttribute("href", "javascript:void(0)");
 }
};

2 Comments

I actually just wanted to change the href to "no-link"
you want to do that on click of image only or its fine vanishing the link on page load itself???

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.