Robert E. McIntosh
Robert E. McIntosh

Reputation: 6125

Get link if user selected text is contained within a link

Okay, so I am trying to figure out how to get a link href if the user has highlightened text is contained within a text...

So for example if the following is a link

<a href="http://www.google.com">Find us on Google</a>

and the user hightlights the text "Google"

<a href="http://www.google.com">Find us on Google</a>

So the question is: After the user highlights text (as in to copy and paste it) they well hit a button and it will return what the link is for the selected text.

I hope I made this clear, wasn't really sure how to phrase it.

Upvotes: 1

Views: 957

Answers (1)

adeneo
adeneo

Reputation: 318182

window.getSelection().anchorNode.parentNode.href;

or in IE:

document.selection.createRange().parentElement().href;

should get the element's href where the text is selected ?

Upvotes: 7

Related Questions