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

javascript - IndexedDB error: Uncaught DataCloneError: Failed to execute 'put' on 'IDBObjectStore': An object could not be cloned -

amazon web services - When using Cognito credentials with AWS in a browser (javascript), keep getting "missing credentials" error -

c# - How to set the datatextfield value instead of the value on a drop down list? -