JQuery change inner text but preserve html

This seems to work just fine.

Live Demo

<html>
    <head>
    </head>
    <body>
        <a href="https://stackoverflow.com/questions/5232862/link.html">Some text <img src="img.jpg" /></a>
    </body>

    </html>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
    <script type="text/javascript">
        $(function(){
            var $link = $('a');
            var $img = $link.find('img'); 
            $link.html('New Text');
            $link.append($img);
        });
    </script>

Leave a Comment