0

How do I get jQuery to return the entire

<div class="loan_officer_apply_now_link"><a href="">APPLY NOW!</a></div>

Currently it only returns the contain "a" element

<a href="">APPLY NOW!</a>

Test Code

$('a').click(function(){
    alert($('.loan_officer_apply_now_link').html());
})

http://jsfiddle.net/gUd9J/1/

Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100
Julian
  • 1,853
  • 5
  • 27
  • 48
  • http://stackoverflow.com/questions/2419749/get-selected-elements-outer-html – Chris Aug 21 '13 at 15:27
  • @Rfvgyhn similar questions but the top solutions for that question are woefully outdated compared to the working answers on this page. – Julian Aug 21 '13 at 15:36

5 Answers5

5

Use the native JS .outerHTML property instead of just the jQuery .html() function. It will make you life a lot easier, instead of trying to do fancy jQuery wraps. You can do this with the following:

$(selector)[0].outerHTML // This is your HTML code
LoveAndCoding
  • 7,857
  • 2
  • 31
  • 55
2

You can also use the element's outerHTML property

$('a').click(function(){
    alert($('.loan_officer_apply_now_link').get(0).outerHTML);
})

Demo: Fiddle

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
1

jQuery doesn't have a built-in function for this.. here's one solution:

http://jsfiddle.net/TvWLL/

$('a').click(function(){
    alert($('.loan_officer_apply_now_link').clone().wrap('<div>').parent().html());
})
Jason P
  • 26,984
  • 3
  • 31
  • 45
0

give a wrapper class to it

<a href="#">Click Me</a><br /><br />
<div class="container">
<div class="loan_officer_apply_now_link"><a href="">APPLY NOW!</a></div>
</div>

$('a').click(function(){
    alert($('.container').html());
})

Working fiddle

Mandeep Jain
  • 2,304
  • 4
  • 22
  • 38
0

It appears you want the parents html:

$(this).parent()[0].outerHTML
Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100