-1

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}
halfer
  • 19,824
  • 17
  • 99
  • 186
James
  • 1,668
  • 19
  • 50
  • remove `.parent()` or add `.find(jquery_selector_for_span)` before `.text()` – Richard Theobald Feb 05 '16 at 03:14
  • 1
    Wrap the text you want to replace in span with a class and target that span. – Matt Feb 05 '16 at 03:14
  • I cant amend the template as its a core template as will be replaced when software is updated hence trying this JS hack / Smarty hack solution. If i remove parent does not work, and only thing i can attach JS to is that span ID so need to select that, and replace the text only within it without removing the other elements – James Feb 05 '16 at 03:16
  • I updated question with what is expected so can better see what i mean – James Feb 05 '16 at 03:17
  • Get a reference to just the text node and update that - various existing Stack Overflow questions can help with that, and I've closed this question as a duplicate of the first one of those that I found. Or first detach the spans, then update the text, then prepend the spans back in. – nnnnnn Feb 05 '16 at 03:46
  • Just just worked something out with looking around :) – James Feb 05 '16 at 03:47

1 Answers1

0

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>
James
  • 1,668
  • 19
  • 50