Searching for the row value of specific data within a table using Javascript

I have a PHP table populated with values, and I want to make two of the table data cells editable. The table headers include: Task, Due Date, Staff, and Department. The values in the table data are retrieved from a database using Laravel Eloquent.

<table class="table table-striped">
          <thead>
               <tr><th scope="col">Task</th><th scope="col">Due Date</th><th scope="col">Staff</th><th scope="col">Department</th></tr>
          </thead>
          @foreach($tasks as $task)

               <input type="text" id="taskid" name="taskid" value="{{$task->id}}" hidden>

               <tr class="tabletd"><td scope="col" >{{$task->task}}</td>
                    <td scope="col" contenteditable="true">
                         <input type="date" name="duedate" id="duedate" class='form-control form-control-sm' value="{{$task->duedate}}" style="width:135px;" >
                    </td>

                    <td scope="col">{{$task->user->name}}</td><td scope="col">{{$task->department->title}}</td>

               </tr></table>   @endforeach
     

I am utilizing AJAX to update these values on the server.

$(document).ready(function(){

          $(document).on('change','#duedate',function(){
               var duedate=$(this).val();
               var id = $('#taskid').val();
               $.ajax({
                    type:'get',
                    url:'{!!URL::to('duedateupdate')!!}',
                    data:{'id':id, 'duedate':duedate},
                    success:function(data){
                         document.getElementById("updatemsg").hidden = false; 
                         setTimeout( function (){
                              document.getElementById("updatemsg").hidden = true; 
                         }, 2000);
                    },
                    error:function(){

                    }
               });
          });
     

The code works fine, but it only updates the first row of the table. How can I extend this functionality to update values in the other rows as well?

Answer №1

The issue arises from using the input id "duedate"

Update the id to a class

<input type="date" name="duedate" class='form-control form-control-sm duedate' value="{{$task->duedate}}" style="width:135px;" >

In your event listener, utilize the class selector:

$(document).on('change','.duedate',function(){

This adjustment should yield the desired outcome

--- revision based on the following comment: -----

Take note of the selector here :
var id = $('#taskid').val();

Once again, refrain from using an id (which can only select one element), and opt for a data value in the input as shown below:

<input type="date" data-task-id="{{$task->id}}" class="duedate" class='form-control form-control-sm' value="{{$task->duedate}}" style="width:135px;" >

You can then reference this in your ajax code:

var id = $(this).data( 'task-id' );

If you encounter issues with data(), use the following approach:

var id = $(this).attr( 'data-task-id' );

Answer №2

After some code adjustments, I have successfully implemented the following updates:

<input type="date" name="duedate"  contenteditable="true" data-id="{{$task->id}}" data-column="duedate"  class="form-control form-control-sm duedate"  value="{{$task->duedate}}" style="width:135px;" >


$(document).on('change','.duedate',function(){
    var duedate = $(this).val();
    var id = $(this).data("id");
    $.ajax({
        type:'get',
        url:'{!!URL::to('duedateupdate')!!}',
        data:{'id':id,'duedate':duedate},
        success:function(data){

        },
        error:function(){

        }
    });
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ways to acquire the reference of the parent component

Within my code, there is a parent component represented as: <Parent> <Child (click)=doSomething()></Child> </Parent> I am looking to establish a reference to the Parent component in order to pass it within a method that will trigg ...

Building an HTML form to generate an array of objects by collecting form data

Hey there, I'm working on an HTML form in React that utilizes form action and FormData. However, when I extract the formData using my method, it shows a structure like this: https://i.stack.imgur.com/nzgLX.png My goal is to have an array of objects ...

Choose an option using jQuery with the ability to navigate to the previous or next

I encountered a bug when I tried to click the next or previous button. Below is the HTML code snippet: $("#nextPage").click(function() { $('#pagination option:selected').next().attr('selected', 'selected'); console.log( ...

Issue with executing Jquery in PUG file: The $ sign is not being recognized despite jQuery being imported

I am encountering an issue where my jQuery code placed inside a pug template is not executing as expected. Despite including the jQuery file, when trying to run a jQuery function, I receive the error below: 40| P 41| ...

Free up MySQL connections within a Promise.All implementation

At the moment, I am facing issues with releasing MySQL connections from a connection pool. Interestingly, when I release connections in a synchronous "for" loop, everything works fine. However, when I attempt to release them asynchronously using Promise.Al ...

The HTTP request is malfunctioning in a different location

I'm facing an issue where my code works in the w3schools code editor, but not on localhost or cpanel host. When I try to run it on another host, it gives me a bad request and doesn't return the answer. Here is the code snippet that I am working ...

Executing the serverless invoke local command produces no output

Attempting to locally run a node lambda for debugging purposes. Utilizing Serverless and the provided launch configuration in vsCode. { "version": "0.2.0", "configurations": [ { "type": "node", "request": "launch", "name": "Launc ...

What is the process of performing numerical calculations using jQuery?

I need to deduct certain input values from the total price. Here's the code snippet: $('.calculate-resterend').click(function(e) { e.preventDefault(); var contant = $('.checkout-contant').val(); var pin = $('.che ...

What could be the reason behind my inability to initiate this PHP file through jQuery/AJAX?

I'm experiencing an issue with a piece of PHP code (located at ../wp-content/plugins/freework/fw-freework.php). <div id="new-concept-form"> <form method="post" action="../wp-admin/admin.php?page=FreeWorkSlug" class="form-inline" role ...

Dynamic Ajax form submission with increasing intervals

There seems to be an issue with my form submission using ajax. When I change the values and submit again, it inserts duplicate rows - 2 on the second attempt, 3 on the third, and so on. I suspect that there might be a problem with how the number is being s ...

Having issues with the POST method in node.js and express when connecting to a MySQL database

My GET method is functioning perfectly I have a database called stage4 and I am attempting to insert values into it from a frontend page The connection is established, I'm using Postman to test it first, but it keeps returning a "404 error" which is ...

Try utilizing querySelectorAll() to target the second item in the list

As I delve into the world of HTML and JS, I came across the document.querySelectorAll() API. It allows me to target document.querySelectorAll('#example-container li:first-child'); to select the first child within a list with the ID 'exampl ...

Objects that are included into an array will end up with an undefined value

Currently, I am in the process of coding a command for my Discord bot that involves adding items to an array of objects stored within a JSON file. To achieve this functionality, I have implemented the following code snippet: let rawdata = fs.readFileSync(& ...

Utilizing React Hooks and Firebase Firestore onSnapshot: A guide to implementing a firestore listener effectively in a React application

SITUATION Picture a scenario where you have a screen with a database listener established within a useEffect hook. The main goal of this listener is to update a counter on your screen based on changes in the database: (Utilizing the useEffect hook without ...

The initial ajax request may sometimes return as 'undefined' during its first call

I have been working on creating a text input help feature similar to a desktop using the datatable.in table with keyboard navigation. In order to achieve this, I am dynamically changing the data source and also altering the header column. I have been succe ...

Error 404: Unable to locate webpack bundle.js

As I dive into learning about Webpack configuration, I keep encountering errors in my console. It appears that my webpack app.bundle.js file is not being found. The page successfully loads with the content of my HTML file displaying, but the app.bundle.js ...

``When multiple elements are clicked, the second click will remove the class

I'm struggling with jQuery. Essentially, I want the first click on "div 1" to add a class to the others and display "lorem ipsum" text, and continue this pattern for the rest. However, if I click on the same div again, the class should be removed and ...

The jquery error NS_ERROR_XPC_BAD_CONVERT_JS is causing issues on Google Chrome while working fine on Firefox

Currently, I am utilizing jQuery to dynamically add fields to a form. These are considered "repeatable" fields since users can click an "add more" button. Here is the code snippet: $(".add-attacker-scores").click(function() { count = count + 1; ...

How is the purpose of nesting functions within functions achieved in nodejs?

Take a look at this example: var tools1 = require('../tools/tools1'); var test_func = function(arg1, arg2, arg3) { var local_var_1 = "lc1"; var local_var_2 = "lc2"; return function(data) { var result = tools1.doSth(local_va ...

Transferring data between various stages of the user interface

As a newcomer to angularJs, I find myself facing an issue with two forms existing in different UI states (URLs) labeled as Step 1 and Step 2. The process requires filling up Step 1 and moving to the next step by clicking the NEXT button, which then leads t ...