ykh
ykh

Reputation: 1815

Get anchor href attribute using JQuery

I have a button with the following javascript function:

function getHyperLink(){
  window.getSelection().anchorNode.parentNode.attributes["0"].nodeValue;
}

What i do is i highlight a text which contains a hyperlink, i then press the button and want go get the link from selected text.

The following above works but i want my code to be in jQuery if possible.

Upvotes: 0

Views: 695

Answers (1)

StuperUser
StuperUser

Reputation: 10850

You can use attr() to get an attribute by its name:

var url = $(setContainingElement).attr('href');

Not sure how you're using it in your code, but you can use jQuery to wrap the native javascript object like so:

var url = $(window.getSelection().anchorNode.parentNode).attr('href');

Upvotes: 2

Related Questions