1

I want to ask my problem hehe: in my case I want to show my data selector from JavaScript to my modal.

From my code, I have done using console.log() to view the data and it works, but it still didn't want to show in my form modal. Please help me where i'm missing it hehe ...

This is my JavaScript

<script> 

          let namaToko = document.getElementById("namaToko").innerHTML;
          let alamatToko = document.getElementById("alamatToko").innerHTML;
          let buttonDetail = document.querySelectorAll('.view-detail');
          buttonDetail.forEach(function(_el)  {
            _el.addEventListener('click', function(e) {
              jQuery('#modalUpdate').modal('show');
              let row = document.querySelector(`[data-row-id="${e.target.dataset.id}"]`);
              let nama = row.querySelector('td:nth-child(2)');
              let alamat = row.querySelector('td:nth-child(3)');
              
              console.log(nama);
              console.log(alamat);
              
              namaToko = nama;
              alamatToko = alamat;

            });
          });

      </script>

This is my form modal and the datatable

<!-- Bagian Edit Form Modal -->
            <div class="modal fade modalUpdate" tabindex="-1" role="dialog" aria-hidden="true" id="modalUpdate">
                <div class="modal-dialog">
                <div class="modal-content">
                <div class="modal-header">
                  <h4 class="modal-title" id="exampleModalLabel">Edit Data Toko</h4>
                  <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                  </button>
                </div>
              <div class="modal-body">
                <form method = "post" action = "library/services/formAction.php">
                  <input type="hidden" name="pageReferrer" value="http://localhost/city/?page=store" >
                  <input type="hidden" name="actionObject" value="toko" >
                  <input type="hidden" name="actionType" value="update" >
                  <div class="form-group">
                    <label for="namaToko" class="col-form-label">Nama Toko</label>
                    <input type="text" class="form-control" id="namaToko" placeholder="Masukkan Nama Toko" name="namaToko" required>
                  </div>
                  <div class="form-group">
                    <label for="alamatToko" class="col-form-label">Alamat Toko</label>
                    <input type="text" class="form-control" id="alamatToko" placeholder="Masukkan Nama Toko" name="alamatToko" required>
                  </div>
                  <div class="modal-footer">
                <button type="submit" class="btn btn-primary" name="submit">Save</button>
              </div>
                </form>
              </div>
                  </div>
              </div>
            </div>
            <!-- Akhir Edit Bagian Modal -->
            

              <!-- /.card-header -->
              <div class="card-body">
                <table id="example1" class="table table-bordered table-striped">
                  <thead>
                  <tr>
                    <th>No</th>
                    <th>Toko</th>
                    <th>Alamat</th>
                    <th>Status</th>
                    <th>Opsi</th>
                  </tr>
                  </thead>
                  <tbody>
                  <?php
                    $n=1;
                    $toko = getDataToko();
                    while ($dataToko = mysqli_fetch_array($toko)) {
                      if($dataToko['status_ot']==1)
                      {
                        $status = "Aktif";
                        $idStatus = "1"; 
                      }
                      if($dataToko['status_ot']==2)
                      {
                        $status = "Tidak Aktif";
                        $idStatus = "2"; 
                      }
                    
                  ?>
                  <tr data-row-id="<?= $dataToko['id_ot'] ?>">
                    <td>
                    <?php echo $n++; ?>
                    </td>
                    <td>
                    <?php echo $dataToko['nama_ot'] ?>
                    </td>
                    <td>
                    <?php echo $dataToko['alamat_ot'] ?>
                    </td>
                    <td>
                    <?php echo $status ?>
                    </td>
                    <td>
                    <button type ="button" class ="view-detail btn btn-success" data-id="<?= $dataToko['id_ot'] ?>"> Edit </button>
                    <button type = "button" class = "btn btn-danger deletebtn"> Delete </button>
                    </td>
                  </tr>
                  <?php
                  }
                  ?>   
                  </tbody>
                </table>
              </div>
              <!-- /.card-body -->
            </div>
            <!-- /.card -->
          </div>
          <!-- /.col -->
        </div>

And this is the picture where i'm click the Edit Button, and the data was showing in the console but didn't show up in my modal form:

enter image description here

13
  • So you want to show data from <td> to form? Commented Sep 24, 2021 at 8:32
  • yeahh you 're right Commented Sep 24, 2021 at 8:34
  • let namaToko = document.getElementById("namaToko"); let alamatToko = document.getElementById("alamatToko"); namaToko.value = nama.innerText; alamatToko.value = alamat.innerText; Commented Sep 24, 2021 at 8:36
  • Am i still need innerHTML on my let namaToko? Commented Sep 24, 2021 at 8:39
  • 2
    Hey @RidhoWisnu if this or any answer has solved your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. Commented Sep 24, 2021 at 9:16

2 Answers 2

1

If you give your form a name you can then set its input values using their name.

<form name="myForm" method = "post" action = "library/services/formAction.php">
const myForm = document.forms.myForm;
myForm.elements.namaToko.value = namaToko;
myForm.elements.alamatToko.value = alamatToko;
Sign up to request clarification or add additional context in comments.

Comments

1

To set value

jQuery("#modalUpdate #namaToko").val(nama.innerText);
jQuery("#modalUpdate #alamatToko").val(alamat.innerText);

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.