Question

After my using Bootstrap editable div I have some like this:

<span>
  Simple text
  <u>Hello!</u>
  Second text.  
</span>

All what I need - get all text from div, but if I use this recursive function, its return only content from elements, but text "Simple text" and "Second text" stay away from this function.

function parseElem(e){
  var r='';
  if(e.children('*').size()>0){
    e.children('*').each(function(){        
        r+=parseElem($(this));
    });
    return r;
  } else {
    return e.html().replace('<br>','\n');
  }    
}

And if I have

<span>
  He<b>ll</b>o!
  <p>
    <span>
      this
      <i>is</i>
      real
    </span>
    example  
  </p>
</span>

I want to leave tags i,u and stroke in returned value.

Was it helpful?

Solution

This should filter the contents of your span down to your requirements. Just add any tags you do not want unwrapped to the :not() selector.

$('span').find(':not(b, i, u)').contents().unwrap();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top