Uder Moreira
Uder Moreira

Reputation: 932

How to get href value inside html object with jQuery?

i got an object like this

 var obj = $("#something");
 alert(obj.html());

and inside obj.html() I have:

 text <a href='link\to\somewhere'> click here </a> text

The question is, how do I get the values link\to\somewhere and click here by using jQuery?

Upvotes: 1

Views: 4512

Answers (4)

Warren Krewenki
Warren Krewenki

Reputation: 3583

Building on what you already have:

var obj = $("#something");
var html = obj.html();

You can then pass the html to jQuery if you want to access it:

var href = $(html).attr('href')

Upvotes: 1

blackpanther
blackpanther

Reputation: 11486

For finding href:

var href = obj.find("a").attr("href");

This is because href is an attribute of the <a> tag and hence the jQuery method attr() can be used.

Upvotes: 2

Mohammad Masoudian
Mohammad Masoudian

Reputation: 3501

for href :

href = obj.find("a").attr("href");

and for click here :

text = obj.find("a").text();

Upvotes: 1

nullability
nullability

Reputation: 10675

var href = obj.find("a").attr("href"), // "link\to\somewhere"
    text = obj.find("a").text();       // "click here"

Upvotes: 4

Related Questions