javascript - Get whole tags including selected tag in jquery -
this question has answer here:
- get selected element's outer html 26 answers
my html content
<div class="div1> div1 </div> <div class="div2> div2 </div> my jquery code is
$("div").click(function(){ alert($(this).html()); }); this return either "div1" or "div2" based on click. whole tags <div class="div1"> div1 </div> "this" object. there anyway that...?
you need outer html of element. can achieve creating temporary clone of element itself. example:
$('div').on('click', function() { alert( $('<div>').append( $(this).clone() ).html() ); }); alternatively, can use built-in outerhtml property, example:
$('div').on('click', function() { alert( this.outerhtml ); });
Comments
Post a Comment