0

I have an app which uses EJS templating to populate data.

In order to add new projects I have made a simple input form which takes all required parameters.

After an input completed an Ajax request being sent, on success I want to inject this snippet into DOM.

In simple words - After new project added I want to display play instantly by injecting into DOM without reloading the page

Is there an elegant way of inserting this div ladder as a template into DOM? It works,

<div class="projects">
    <div class="projectHeader">
          <div class="projectTitle">
            <span>
              <a data-toggle="modal" data-target="#editDeadLineModal">
                <i data-id="<%=project.id%>" class="projectDeadline far fa-calendar-alt fa-2x" data-toggle="tooltip" data-placement="top" title="Set Deadline"></i>
              </a>
            </span>
            <h5 class="projectName <%=project.id%>" data-toggle="tooltip" data-placement="top" title="Deadline <%=deadline%>" style="align-items: center;">
              <%=project.name%>
            </h5>
            <%}%>
          <div class="projectButtons">
            <span data-toggle="tooltip" data-placement="top" title="Edit Project Title">
              <a data-toggle="modal" data-target="#editProjectTitleModal">
          <i id="editProjectName" class="editProject fas fa-pencil-alt" data-name="<%=project.name%>" data-id="<%=project.id%>"></i>
            </a>
            </span>
    
            <i class="separatorDash fas fa-minus"></i>
    
            <span data-toggle="tooltip" data-placement="top" title="Delete Project">
              <a data-toggle="modal" data-target="#deleteProjectModal">
                <i id="deleteProject>" class="deleteProject far fa-trash-alt" data-id="<%=project.id%>"></i>
              </a>
            </span>
          </div>
        </div>
      </div>
</div>

What I have tried is recreating the entire div ladder in string and append it to parent node.

Like this:

    // Add new Project
    $('#addNewProjectBtn').on("click", function() {
    $("#newProjectModal").on('show.bs.modal', function() {
      $(".confirmNewList").on("click", function(event) {
          var url = '/addNewList';
          var newListTitle = $("#newListNameInput").val()
          event.preventDefault();
          $.post({
            url: url,
            data: {
              listName: newListTitle
            },
            success: function(result) {
              $("#newProjectModal").modal('hide')
              $("#newListNameInput").val('');
              var id = result.data.id
              var name = result.data.name
                  
//Append new project 
                  $(".projects").append("<div class='project col-4' id='project" + id + "'> <div class='projectHeader'> <div class='projectTitle'> ...and so on until the end")
                },
                error: function(err) {
                  console.log(err);
                }
              })
            }
          }
        })
       })
  })

In simple words - After new project added I want to display play instantly by injecting into DOM without reloading the page

Is there an more elegant and specially efficient way of inserting this div ladder as a template into DOM?

The method which I have tried above - works, But on attempt to interact with it by calling modals - modals do not get it's data-*, as well the bootstrap tooltips don't work.

0

2 Answers 2

1
  1. you can try create new html file instead, and append like this in your page

    $.get("yourfile.html", function (data) {
      $("#appendToThis").append(data);    // or use .html();
    });
    
  2. OR you can directly pass this HTML structure from your backend, so you can directly use append function.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you for the tip. Nevertheless I have found a solution below
0

After some research I discovered that the event handler can be delegated.

Since the html snippet I am trying to append to projects is added after document.ready (document is loaded and handlers are bind) the only thing required to make the new snippet be active on event is to delegate the event handler to the parent of element that is appended.

Like this :

$("body").delegate("#addNewProjectBtn", "click", function() {
$("#newProjectModal").on('show.bs.modal', function() {
      $(".confirmNewList").on("click", function(event) {
          var url = '/addNewList';
          var newListTitle = $("#newListNameInput").val()
          event.preventDefault();
          $.post({
            url: url,
            data: {
              listName: newListTitle
            },
            success: function(result) {
              $("#newProjectModal").modal('hide')
              $("#newListNameInput").val('');
              var id = result.data.id
              var name = result.data.name
                  
                 //Append new project 

                  $(".projects").append("<div class='project col-4' id='project" + id + "'> <div class='projectHeader'> <div class='projectTitle'> ...and so on until the end")
                },
                error: function(err) {
                  console.log(err);
                }
              })
            }
          }
        })
       })
  })

By delegating the event handler to it's parent - the event handler is preserved in the parent.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.