Search code examples
javascriptphpjqueryhtmlsmarty

jQuery - replacing spans parent text is removing the span elements?


I have some code replacing the text in the TD but it's also removing the other elements within the TD such as the spans, is there any way just to replace the text?

Before replacing

<td>
    <span alt="Expand the sublist of items" title="Expand the sublist of items" id="on_user_81257937_1" class="cm-combination-carts" onclick="Tygh.$.ceAjax('request', 'admin.php?dispatch=cart.cart_list&amp;user_id=81257937&amp;c_company_id=1', {result_ids: 'cart_products_81257937_1,wishlist_products_81257937_1', caching: true});"><span class="exicon-expand"></span></span>
    <span alt="Collapse the sublist of items" title="Collapse the sublist of items" id="off_user_81257937_1" class="hidden cm-combination-carts"><span class="exicon-collapse"></span></span>

    Unregistered customer        
</td>

After replacing with my JS

<td>James Murphy</td>

What is expected

<td>
    <span alt="Expand the sublist of items" title="Expand the sublist of items" id="on_user_81257937_1" class="cm-combination-carts" onclick="Tygh.$.ceAjax('request', 'http://beanbags.ambientlounge.com/admin.php?dispatch=cart.cart_list&amp;user_id=81257937&amp;c_company_id=1', {result_ids: 'cart_products_81257937_1,wishlist_products_81257937_1', caching: true});"><span class="exicon-expand"></span></span>
    <span alt="Collapse the sublist of items" title="Collapse the sublist of items" id="off_user_81257937_1" class="hidden cm-combination-carts"><span class="exicon-collapse"></span></span>

    James Murphy       
</td>

Smarty / JS

{assign var="ac_firstname" value=$customer.user_id|fn_get_ac_firstname}
{assign var="ac_lastname" value=$customer.user_id|fn_get_ac_lastname}

{if !empty($ac_firstname) || !empty($ac_firstname)}
    <script type="text/javascript">

        $(document).ready(function(){

            $('#off_user_{$customer.user_id}_1').parent().text('{$ac_firstname} {$ac_lastname}');

        });

    </script>
{/if}

Solution

  • Solution was:

    <script type="text/javascript">
    
        $(document).ready(function(){
    
            $('#off_user_{$customer.user_id}_1').parent().contents().last()[0].textContent="{$ac_firstname} {$ac_lastname}";
    
        });
    
    </script>