jquery - Replace text in a tag leaving child tag intact -


this question has answer here:

i have rather interesting issue. content being build dynamically , creating following scenario.

<button id="something">     <i class="fa fa-refresh"></i>     button text </button> 

what i'm trying replace "some button text" while preserving font awesome tag. i've tried regex , on, kind of stuck.

as note, i've tried .text() , replace text, removes tag too.

you can use contents() getting descendant including textnode . iterate on result using each() , update nodvealue

$('#something').contents().each(function() {    if (this.nodetype === 3)      this.nodevalue = $.trim(this.nodevalue).length ? 'new value' : '';      // if want remove element use 'this.remove()'  })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>  <button id="something">    <i class="fa fa-refresh"></i>    button text  </button>


Comments