Jquery getting attr href from string

Discussion in 'jQuery' started by ernest1a, Jul 13, 2010.

  1. #1
    I am trying to get a href attr from variable msg. The problem is that the msg consists of not only <a element but it contains also <p element and because of that attr doesn't give any value.

    var msg="<p><a href='/users/'>Username</a> Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
    var href=$(msg).attr('href');

    Does anybody any idea how I can get the value of href from the above example?

    Thank you
     
    ernest1a, Jul 13, 2010 IP
  2. ernest1a

    ernest1a Guest

    Messages:
    182
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I found solution, it is easy:)
    var href = $(msg).find('a').attr('href');
     
    ernest1a, Jul 13, 2010 IP
  3. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    lol, gotta love JQuery :)
     
    Deacalion, Jul 13, 2010 IP