0

This is my table.

       <table id="project-table" class="table table-striped table-inverse table-responsive">
                        <caption>Projects</caption>
                        <thead class="thead-inverse">
                            <tr>
                                <th scope="col">#</th>
                                <th scope="col">Project name</th>
                                <th scope="col">Description</th>
                                <th scope="col">Estimated time (min)</th>
                                <th scope="col">Actual time (min)</th>
                                <th scope="col">Add task</th>
                                <th scope="col">Delete project</th>
                            </tr>
                        </thead>
                        <tbody id="project-body">


                        </tbody>
                    </table>

This table is filled with data from an AJAX call.

$.ajax({
    method: 'GET',
    dataType: 'json',
    data: {
      functionToCall: 'project',
    },
    url: 'http://localhost/WBS/php/api/requests/get.php',
    success: (response) => {
      // from https://stackoverflow.com/questions/733314/jquery-loop-over-json-result-from-ajax-success
      $.each(response, function () {
        $.each(this, function (index, value) {
          $('#project-body').append(
            `
            <tr>
                <td>
                  <input class="form-control" type="hidden" name="projectid" id="projectid"  value="${value.projectid}">
                </td>
                <td>
                  <input class="form-control" type="text" name="projectName" id="projectName" value="${value.title}">
                </td>
                <td>
                  <input class="form-control" type="text" name="description" id="description"  value="${value.description}">
                </td>
                <td>
                  <input class="form-control" type="text" name="estimatedTime" id="estimatedTime"  value="${value.Estimated_time}">
                </td>
                <td>
                  <input class="form-control" type="text" name="actualTime" id="actualTime"  value="${value.Actual_time}">
                </td>
                <td>
                  <a id="addTask" class="btn btn-info" href="Overview.html?id=${value.projectid}" role="button">
                    <i class="fa fa-angle-right" aria-hidden="true"> </i> Add task
                  </a>
                </td>
                <td>
                  <button type="button" id="deleteProject" name="deleteProject" class="btn btn-danger">
                    <i class="fa fa-angle-right" aria-hidden="true"> </i> Delete project
                  </button>
                </td>
            </tr>

            `
          );
        });
      });
    },
    error: () => {
      console.error('Something went wrong with the getProjects function');
    },
  });
}

I would like to use the value from this input, to be used in a AJAX delete request.

<td>
 <input class="form-control" type="hidden" name="projectid" id="projectid"  value="${value.projectid}">
</td>

The code I use to get said ID is this

  let id = $(this).closest('tr').find('#projectid').val();

But debugging this in my browser returns undefined.

I tried changing the ajax url to a hardcoded value and the data is received at the back-end.

niels van hoof
  • 469
  • 4
  • 19
  • What is `$(this)` here? and have you verified if you have a valid value actually for the `projectid` in the UI after render? – palaѕн Apr 29 '20 at 07:52
  • `projectid` has a value corresponding to an id from the database, I checked this in the browsers debug tools and it has a id in the value parameter. I call the delete ajax call on the `#deleteProject` button, should `$(this)` not refer to the clicked button and from their i can search for the table? – niels van hoof Apr 29 '20 at 09:17

0 Answers0