0

$(document).ready(() => {
  $("#open-modal").click((event) => { //Opening Edit Model And Assigning Row Values In its form
    let rowdata = {
      activeStatus: true,
      description: "VIM Admin Policy",
      isDeleted: false,
      name: "page2VIM",
    };
    console.log(rowdata);
    $("#policyName").attr("value", rowdata.name);
    $("#policyDescription").attr("value", rowdata.description);
    $("#policyDeleteStatus").attr("value", rowdata.isDeleted);
    $("#policyActiveStatus").attr("value", rowdata.activeStatus);
    $("#hidden-button").show();
    $("#editModal").show();
  });
  $("#editModal-close").click((event) => {
    $("#editModal").hide();
  });
});
<!DOCTYPE html>
<html>

<head>
  <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-p34f1UUtsS3wqzfto5wAAmdvj+osOnFyQFpp4Ua3gs/ZVWx6oOypYoCJhGGScy+8" crossorigin="anonymous"></script>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-wEmeIV1mKuiNpC+IOBjI7aAzPcEZeedi5yW5f2yOq55WWLwNGmvvx4Um1vskeMj0" crossorigin="anonymous">

</head>

<body>
  <!-- Bootstrap5 Modal Componant -->
  <div class="modal" id="editModal" tabindex="-1" aria-labelledby="editModalLabel" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="editModalLabel">Edit Policy</h5>
        </div>
        <div class="modal-body">
          <form>
            <div class="mb-3">
              <label for="policyName" class="form-label">Name</label>
              <input type="text" class="form-control" id="policyName" aria-describedby="emailHelp" value="Name">
              <!-- <div id="emailHelp" class="form-text">We'll never share your email with anyone else.</div> -->
            </div>
            <div class="mb-3">
              <label for="policyDescription" class="form-label">Description</label>
              <input type="text" class="form-control" id="policyDescription" value="Description">
            </div>
            <div class="mb-3">
              <label for="policyDeleteStatus" class="form-label">Delete Status</label>
              <input type="text" class="form-control" id="policyDeleteStatus" value="Delete">
            </div>
            <div class="mb-3">
              <label for="policyActiveStatus" class="form-label">Active Status</label>
              <input type="text" class="form-control" id="policyActiveStatus" value="Active">
            </div>
          </form>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-bs-dismiss="modal" id="editModal-close">Close</button>
          <button type="button" class="btn btn-primary" id="policyUpdate">Update</button>
        </div>
      </div>
    </div>
  </div>
  <button class="btn btn-primary" id="open-modal">Open Modal</button>
  <button class="btn btn-secondary" style="display: none;" id="hidden-button">A Button</button>
  <script>
  </script>
</body>

</html>

The problem I am facing is that once I open the modal and make some changes in input box of form then close the modal and reopen it it loads with that changed data.What I want is that every time Modal opens it opens with that default data i have put inside it. Is there a way to achieve this?

drake1994
  • 69
  • 11
  • 2
    Use `.val()` to asssign value to your input i.e : `$("#policyName").val(rowdata.name);` same for other inputs as well – Swati May 11 '21 at 06:52
  • Thanks worked like a charm.Mind telling me theory behind me or a place where i can get the theory behind it. – drake1994 May 11 '21 at 07:35
  • Have a look [here](https://stackoverflow.com/questions/8312820/jquery-val-vs-attrvalue) – Swati May 11 '21 at 11:46

0 Answers0