javascript - Approach to avoid DOM elements from being removed on .text() -


i have below anchor element contains smaller image in it.

<a href="#" id="mybutton" class="btnclass">   <img src="images/arrow2.png"/> </a> 

from javascript set text of "mybutton" element using,

$("#mybutton").text("submit form"); 

i understand .text() set contents of "mybutton" , remove existing img element.

i want understand best approach avoid img element being removed dom.

p.s i'm mentioning 1 simple approach in answer. please suggest other possible solutions.

we can add additional span element below,

<a href="#" id="mybutton" class="btnclass">   <span id="texthere"></span> <!--new span-->   <img alt="img not avaiable" src="images/arrow2.png"/> </a> 

and set text on added span.

$("#texthere").text("submit form"); 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

c# - Retrieve google contact -

javascript - How to insert selected radio button value into table cell -