javascript - Get whole tags including selected tag in jquery -


this question has answer here:

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

Popular posts from this blog

java - Unable to make sub reports with Jasper -

scala - play framework: Modules were resolved with conflicting cross-version suffixes -

Save and close a word document by giving a name in R -