Search code examples
javascripthtmlhyperlinkhref

Can I use javascript:this.child.src for a link's href?


I have an image like so:

<a href="javascript:this.child.src" target="_blank"><img src="http://placekitten.com/g/300/200"/></a>

As you can see, I want the link to open the <img>'s location in a new tab. I tried this.child.src and a few other things. How would I make the href get the location of the image, and open it in a new tab?

Also, is there a resource where I can find a list of the valid this. values?


Solution

  • If possible I would place the <img src /> directly in href and NOT use javascript for this. If you really need to do in javascript why so ever you could do with onclick. But it's not nice at all for different reasons..

    <a 
        href="javascript:;" 
        onclick="window.open(this.childNodes[0].src,'_blank');"
    >
         <img src="http://placekitten.com/g/300/200"/>
    </a>
    

    http://jsfiddle.net/0hmtcvtm/