html - jquery remove nodes and keep children -


i ashamed post question have not managed use of unwrap or replacewith methods in jquery need.

my problem simple : need remove nodes (jquery selectors) of html code without losing children of these nodes.

here jsfiddle demonstrate result of unsightly code used reach goal https://jsfiddle.net/uddaeh1u/15/ (yes it's works...)

// var content : html source code of wysiwyg  var result = ''; $(content).contents().each(function(){     var addcontent = '';     // textnode     if(this.nodetype == 3) {         // text node         result+= $(this).text();     } else if(this.nodetype == 1 && $(this).hasclass('atwho-inserted')) {         // if object node target class         // keep it's contents (means ".atwho-inserted" not kept)         result+= $(this).html();     } else {         // in other case keep entirely         result+= this.outerhtml;     } }); 

could find me better code (with unwrap method) ?

thank lot :)

i think 1 please you. notice modification of var tmp

$(document).ready(function(){    var content = $('.start').html();    $('.startcode code').text(content);        var tmp = $(content);    $('.atwho-inserted', tmp).children().unwrap();    var result = tmp[0].outerhtml; // or tmp.html(); lose outermost tag        $('.result').html(result);    $('.resultcode').text(result);  });
body{    padding:10px;  }  .atwho-inserted{    background-color:red;  }  pre code, .wrap{    white-space: pre-line;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>      <div class="container">    <div class="row">      <div class="col-xs-6">        <div class="well">all selectors ".atwho-inserted" in "background-color:red" , have removed without losing children.</div>        <hr/>        <div class="start">          <p>            <span class="atwho-inserted" data-atwho-at-query="@fac" contenteditable="false">              <span class="user_mention" data-identifier="8930">@facticeuserr</span>            </span>            <br /> lorem ipsum text            <span class="atwho-inserted" data-atwho-at-query="#acc" contenteditable="false">              <span class="tag_mention" data-identifier="484">#accessibilité</span>            </span>            <br>            <img src="http://lorempixel.com/100/100/" data-filename="3">            <br />            <b>hello</b>            <br>            <span class="atwho-inserted" data-atwho-at-query="#acc" contenteditable="false">              <span class="tag_mention" data-identifier="653">#accompagnement</span>            </span>          </p>        </div>        <pre class="startcode"><code></code></pre>      </div>      <hr/>      <div class="col-xs-6">            <div class="well">here result<br /> it's works code not optimized</div>        <hr/>        <div class="result"></div>        <pre class="resultcode"><code></code></pre>      </div>    </div>  </div>


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -