Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery Clone() and replace text

I have an big table I need to create multiple times in the same page, but I'm not sure have to do. I thought the easiest way is to have the table as an partial view hidden on the page, and each time I need it I will clone it from my jQuery code. I know have to clone it, but I'm not sure have to insert the text into the specific span's, before I insert it.

This is a small example of my table. Can somebody help me with this problem

 < div id="ToClone">
            <table >
                <tr>
                    <td>
                        <table>
                            <tr><td><b>MMS_RepairLabel:</b></td><td align="right"><span id="RepairId"></span></td></tr>
                            <tr><td><b>MMS_MWTLabel:</b></td><td align="right"><span id="MWT"></span></td></tr>
                            <tr><td><b>MMS_MDTLabel:</b></td><td align="right"><span id="MDT"></span></td></tr>
                        </table>
                    </td>
                </tr>
            </table>
        </div>
        <script>
            var History = (function () {
                $(function () {
                    var tblClone = $("#ToClone").clone();
                });
            }());
        </script>
like image 894
mortenstarck Avatar asked Mar 17 '26 11:03

mortenstarck


1 Answers

You will need to resolve the problem of duplicate ids first. I suggest changing them to classes.

I recommend a little trick of using a dummy script block for your template. This is great for maintenance and browsers just ignore the unknown type so you can insert any elements:

<script id="ToClone" type="text/template">
    <table>
        <tr>
            <td>
                <table>
                    <tr>
                        <td><b>MMS_RepairLabel:</b>
                        </td>
                        <td align="right"><span class="RepairId"></span>
                        </td>
                    </tr>
                    <tr>
                        <td><b>MMS_MWTLabel:</b>
                        </td>
                        <td align="right"><span class="MWT"></span>
                        </td>
                    </tr>
                    <tr>
                        <td><b>MMS_MDTLabel:</b>
                        </td>
                        <td align="right"><span class="MDT"></span>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
</script>

Then clone with your inserted values like this:

    var $clone = $($('#ToClone').html());
    $('.RepairId', $clone).text(repairIdValue);
    $('.MDT', $clone).text(mdtValue);
    $('.MWT', $clone).text(mwtValue);
    // Do something with the clone

JSFiddle: http://jsfiddle.net/TrueBlueAussie/381ugdvr/1/

like image 61
Gone Coding Avatar answered Mar 19 '26 00:03

Gone Coding



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!