New Item not being added to JavaScript collection

Hello, I'm new to JavaScript and attempting to make a component to allow users to add URL links to a collection that will be saved to a database.
With my attached code I'm having issues with my _addToLinksList function in that the new link is not added to my _linksStorage collection. When I step through the  _addToLinksList function the expected values are present but the new link is not added.

I could use some pointers as to why and perhaps some help in fixing the issue. Any help is appreciated.

 function LinksManager() {

            var hdnSelectedLinksSelector = "#" + "<%=hdnSelectedLinks.ClientID%>";
            var tblLinksListSelector = "#tblLinks";
            var delLinkSelector = ".delLink";
            var modLinkSelector = ".modLink";
            var addLinkSelector = "#addLinksLink";
            var hdnLinkIdClassSelector = "input.linkId";
            var modifyDialogSelector = "#linksDialog";
            var linkNameSelector = "#linkName";
            var linkUrlSelector = "#linkUrl";
            var linksListItemClassSelector = ".linksListItem";
            var linksListItemTemplateSelector = ".linksListItem-template";
            var tdLinksListContainerSelector = "tdLinksListContainer";
            var _currentLinkForModifyId;

            var _linksStorage = [];

            var _dialogValidationManager = new ValidationManager(undefined, "linksDialogErrorMessages");
            _dialogValidationManager.forseOnChange = false;

            var _linksValidationManager = new ValidationManager(undefined, globalValidationManager.getErrorMessageContainerSelector().substring(1));
            new RequiredHiddenValidator(hdnSelectedLinksSelector, tdLinksListContainerSelector, undefined, _linksValidationManager);

            function _initLinksStorage() {
                var linksList = JSON.parse($(hdnSelectedLinksSelector).val());
                if (linksList) {
                    for (var i = 0; i < linksList.length; i++) {
                        _linksStorage[linksList[i].Id] = linksList[i];

            function getLinksStorageIds() {
                var ids = [];
                for (var k in _linksStorage) {
                return ids;

            function _onDelLinkClick() {
                var $listItemTr = $(this).closest(linksListItemClassSelector);
                var lnkId = $listItemTr.find(hdnLinkIdClassSelector).val();
                delete _linksStorage[lnkId];


            function _openDialog(linkForModifyId) {
                _currentLinkForModifyId = linkForModifyId;

            function _setDialogFields(linkForModifyId) {
                var lnk = _linksStorage[linkForModifyId];

            function _getLinkFromDialogFields() {
                return {
                    name: $(linkNameSelector).val(),
                    url: $(linkUrlSelector).val()


            function _clearDialogFields() {

            function _addToLinksList(lnk) {
                var $listTr = $(linksListItemTemplateSelector).clone();
                $listTr.attr("class", linksListItemClassSelector.substring(1));
                var $sortedTrs = $(linksListItemClassSelector).sort(_customSort);

            function _customSort(a, b) {
                var nameA = $(linkNameSelector, $(a)).text().toLowerCase();
                var nameB = $(linkNameSelector, $(b)).text().toLowerCase();
                return nameA > nameB ? 1 : -1;

            function _onDialogApply() {
                if (!_dialogValidationManager.validate()) {

                var lnk = _getLinkFromDialogFields();

                _linksStorage[] = lnk;
                if (!_currentLinkForModifyId) {

            function _initDialog() {
                    autoOpen: false,
                    width: 400,
                    position: ['center', 100],
                    modal: true,
                    buttons: {
                        "Apply": _onDialogApply,
                        "Cancel": function () { $(this).dialog("close"); }
                    create: function () {
                        new RequiredValidator(linkNameSelector, undefined, _dialogValidationManager);
                        new RequiredValidator(linkUrlSelector, undefined, _dialogValidationManager);
                    open: function () {
                        if (_currentLinkForModifyId) {
                        } else {

            return {
                onPageLoaded: function () {
                    $(tblLinks).on("click", delLinkSelector, _onDelLinkClick);
                    $(tblLinks).on("click", modLinkSelector, function () {
                    $(addLinkSelector).click(_openDialog.bind(this, null));
                saveLinksStorage: function () {
                    var lnkArray = $.map(_linksStorage, function (res) {
                        return res;
                    var data = lnkArray.length === 0 ? null : JSON.stringify(lnkArray);

                getRequiredValidationManager: function () {
                    return _linksValidationManager;

<td id="tdLinksListContainer" colspan="2" valign="top">
                                <span class="markedOutText">Contact Links:&nbsp;</span>
                                <div style="float: right;"><a id="addLinksLink" href="#">Add Link</a></div>
                                <input type="hidden" runat="server" id="hdnSelectedLinks" />
                                <table id="tblLinks" width="100%">
                                        <col width="80%" />
                                    <tr class="subLabel">
                                        <td class="tdAction">Actions</td>
                                        <%foreach (var lnk in Links)
                                    <tr class="linksListItem<%=lnk.Id==0 ? "-template" : string.Empty  %>">
                                            <a target="_blank" href="<%=lnk.Url%>"><%=lnk.Name%></a>

                                            <input type="hidden" class="linkId" value="<%=lnk.Id %>" />
                                        <td class="tdAction"><a class="delLink" href="#"><%=AppResources.Resources.Common_DelLink%></a>
                                            <a class="modLink" href="#"><%=AppResources.Resources.Common_ModLink%></a></td>

                                    <% } %>

<%--Links Dialog--%>
    <div id="linksDialog" class="ui-helper-hidden" title="Add / Modify Link">
        <div class="linksDialogErrorMessages"></div>
        <table id="tblLink" width="100%">
                    <input type="text" id="linkName" class="text required" />
                    <input type="text" id="linkUrl" class="text required" />

Open in new window

Scott BaldridgeAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
var linksListItemTemplateSelector = ".linksListItem-template";
var $listTr = $(linksListItemTemplateSelector).clone();

Open in new window

I can't find the .linksListItem-template item in the source you provided.

What might be happening is that your template is hidden (display: none) but when you add your links you are not showing the cloned item after appending it. So try


Open in new window

Scott BaldridgeAuthor Commented:
Hello Julian, sorry it took so long to get back to you. .linksListItem-template is near the bottom.

 <tr class="linksListItem<%=lnk.Id==0 ? "-template" : string.Empty  %>">

I tried this to see what would happen if I did this:    $listTr.appendTo(tblLinksListSelector).show();
and what it does is duplicate the existing rows. It does not add the new row.
I do not see what the new row(lnk) is being added to the collection _linksStorage so I attempted to add in this way  _linksStorage.push(lnk);
I see that the new row is in the collection but it does not appear on the screen.

 What am I missing here?
Julian HansenCommented:
What am I missing here?
Pretty sure it is the show()
Having looked at your code I see you are using appendTo which means the .show needs to be appled to the principle object - not the parameter like this


Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.