Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
 Hi
I have a table and a form
I created a button in the table with the name Edit
By pressing this button, the table row information is transferred to the form so that it can be edited

The problem I have is that
How to return the edited information to the previous row and column by clicking the update button 


What I have tried:

<!DOCTYPE html>
<html>
<title>W3.CSS</title>
<meta >
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.25/css/jquery.dataTables.css">
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.25/js/jquery.dataTables.js"></script>
<body>


<div class="w3-col s4 ">
    <div class="w3-container w3-blue w3-card">
        <h2>Input Form</h2>
    </div>
    <form class="w3-container w3-card" id="form">

        <p>
            <label>First Name</label>
            <input class="w3-input" type="text" id="name"></p>
        <p>
            <label>Last Name</label>
            <input class="w3-input" type="text" id="last_name"></p>
        <p>
            <label>Email</label>
            <input class="w3-input" type="email" id="email"></p>
        <P><button type="button" class="w3-btn w3-blue" id="save">Save</button></p>
    </form>
    <br>
    <div class="container">
        <div class="row">
            <div class="col-sm-6">
                <h2> personal information </h2>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-6">
                <table id="table" class="sortable-theme-dark">
                    <thead>
                    <tr>
                        <th>name</th>
                        <th>last name</th>
                        <th>email</th>
                        <th>Delete</th>
                    </tr>
                    </thead>
                    <tbody id="tbody"></tbody>
                </table>
            </div>
        </div>
    </div>

</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<script>

    $(document).ready(function(){
        
        $('#save').click(
            function(){


                if($("#name").val() != null && $("#last_name").val() != ''){

                    $("tbody").append("<tr>" +"<td>" +$('#name').val()+ "</td>" +"<td>" + $('#last_name').val()+ "</td>" +"<td>" +$('#email').val()+ "</td>" + "<td>"  + "</td>" + "<td>" + "<button type='button' id='dell'>"+"Remove"+"</button>" + "</td>"
                        + "<td>" + "<button type='button' id='edit'>"+"edit"+"</button>" + "</td>"+"</tr>");};

                $(form).trigger("reset")
            })
        $(document).on('click','#dell',function(){
            $(this).parent().parent().remove();
        })

        $("#table").on('click','#edit',function(){
            $("form").append("<button type='button' id='update' class='w3-btn w3-blue'>"+"update"+"</button>");
            var currentRow=$(this).closest("tr");
            var col1=currentRow.find("td:eq(0)").text();
            var col2=currentRow.find("td:eq(1)").text();
            var col3=currentRow.find("td:eq(2)").text();
            $("#name").val(col1);
            $("#last_name").val(col2);
            $("#email").val(col3);
           

        });


    })
</script>




</body>
</html>
Posted
Updated 11-Aug-21 16:59pm
v2

1 solution

Please can u try this
$("table tbody").find($(currentRow)).replaceWith(new_row);


sample code below, please change as per you need.

$(document).on('click', 'span.editrow', function () {
                        var currentRow=$(this).closest("tr");
						var col1=currentRow.find("td:eq(0)").text();
						var col2=currentRow.find("td:eq(1)").text();
						var col3=currentRow.find("td:eq(2)").text();
						$("#name").val(col1);
						$("#last_name").val(col2);
						$("#email").val(col3);
                        });

var new_row= "<tr>" +"<td>" +$('#name').val()+ "</td>" +"<td>" + $('#last_name').val()+ "</td>" +"<td>" +$('#email').val()+ "</td>" + "<td>"  + "</td>" + "<td>"  + "</td>"
                        + "<td>"


if(currentRow){
                               
                      $("table tbody").find($(currentRow)).replaceWith(new_row);
                                  currentRow = null;
                                 }
 
Share this answer
 
Comments
Abolfazl 2021 17-Aug-21 11:23am    
thank for your Solution
but i have another problem
when i click on edit
Every time I click on edit, a new button is created, how should this be solved ??

And how can the submit button be disabled or not work when editing the form so that if someone clicks wrong, a new row and column will not be created
Abolfazl 2021 17-Aug-21 11:23am    
And that I have all the problems like transferring information to the table and returning the information to the form with JavaScript
Can you help?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900