CodeIn House

  • Laravel
  • WordPress
  • jQuery
  • Javascript
  • Contact
Home   HTML5   How to Edit Selected Row From HTML Table Using Javascript ?

How to Edit Selected Row From HTML Table Using Javascript ?

June 19, 2021 by SNK

In this tutorial we are going to lean how to edit selected row from HTML table using JavaScript. We are going to use completely native JavaScript and no any other native library like jQuery.

Using JavaScript edit table row using popup we are going to need to create a popup while editing so to do, that we have to create one with HTML & CSS but bootstrap provides a modal class which we can easily create such type of popup in blink of an eye.

We are going to use bootstrap in order to create one and perform the edit selected row from html table.

Let’s get started.

How to Edit Selected Row From HTML Table Using JavaScript?

So, to get started we first have to create an simple table with some records on it and create a popup model which with some of the table fields in order to edit update and delete. I have prepared some codes below to illustrate this with an example.

HTML Table
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Edit Selected ROW From HTML Table Using Javascript</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"/>
</head>
<body>
<div class="container">
    <table class="table table-striped">
        <thead>
        <tr>
            <th scope="col">id</th>
            <th scope="col">Name</th>
            <th scope="col">Email</th>
            <th scope="col">Phone</th>
            <th scope="col">Address</th>
            <th scope="col">Action</th>
        </tr>
        </thead>
        <tbody>
        <tr id="row-1">
            <th scope="row">1</th>
            <td class="name">Mark</td>
            <td class="email">mark@mail.com</td>
            <td class="phone">+99-2312312311</td>
            <td class="address">New-york</td>
            <td>
                <a onclick="toggleModal(this, 1)">Edit</a>
                <a onclick="removeRow(this)">Delete</a>
            </td>
        </tr>
        <tr id="row-2">
            <th scope="row">2</th>
            <td class="name">Lara</td>
            <td class="email">lara@mail.com</td>
            <td class="phone">+99-2312312311</td>
            <td class="address">california</td>
            <td>
                <a onclick="toggleModal(this, 2)">Edit</a>
                <a onclick="removeRow(this)">Delete</a>
            </td>
        <tr id="row-3">
            <th scope="row">3</th>
            <td class="name">Josef</td>
            <td class="email">josef@mail.com</td>
            <td class="phone">+99-2312312311</td>
            <td class="address">Texas</td>
            <td>
                <a onclick="toggleModal(this, 3)">Edit</a>
                <a onclick="removeRow(this)">Delete</a>
            </td>
        </tr>
        </tbody>
    </table>
</div>
 
<!-- Creating a popup modal -->
<div class="modal" tabindex="-1" role="dialog" id="exampleModal">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Edit Table</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <div class="form-group">
                    <label for="name">Name</label>
                    <input type="text" id="name" class="form-control">
                </div>
 
                <div class="form-group">
                    <label for="email">Email</label>
                    <input type="text" id="email" class="form-control">
                </div>
 
                <div class="form-group">
                    <label for="phone">Phone</label>
                    <input type="text" id="phone" class="form-control">
                </div>
 
                <div class="form-group">
                    <label for="address">Address</label>
                    <input type="text" id="address" class="form-control">
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="saveInfo()">Save changes</button>
                <button type="button" class="btn btn-secondary" onclick="closeModal()">Close</button>
            </div>
        </div>
    </div>
</div>
<div class="modal-backdrop fade show" id="backdrop" style="display: none;"></div>

Now let’s write the JavaScript code.

JavaScript Code
JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<script>
    let tableRowElement;
    function toggleModal(element) {
 
        tableRowElement = element.parentElement.parentElement;
        const name = tableRowElement.getElementsByClassName('name')[0].innerHTML;
        const email = tableRowElement.getElementsByClassName('email')[0].innerHTML;
        const phone = tableRowElement.getElementsByClassName('phone')[0].innerHTML;
        const address = tableRowElement.getElementsByClassName('address')[0].innerHTML;
 
        document.getElementById('name').value = name;
        document.getElementById('email').value = email;
        document.getElementById('phone').value = phone;
        document.getElementById('address').value = address;
 
        openModal();
    }
 
    function saveInfo() {
        const name = document.getElementById('name').value;
        const email = document.getElementById('email').value;
        const phone = document.getElementById('phone').value;
        const address = document.getElementById('address').value;
 
        tableRowElement.getElementsByClassName('name')[0].innerHTML=name;
        tableRowElement.getElementsByClassName('email')[0].innerHTML=email;
        tableRowElement.getElementsByClassName('phone')[0].innerHTML=phone;
        tableRowElement.getElementsByClassName('address')[0].innerHTML=address;
 
        closeModal();
    }
 
    function openModal() {
        document.getElementById("backdrop").style.display = "block"
        document.getElementById("exampleModal").style.display = "block"
        document.getElementById("exampleModal").classList.add("show");
    }
 
    function closeModal() {
        document.getElementById("backdrop").style.display = "none"
        document.getElementById("exampleModal").style.display = "none"
        document.getElementById("exampleModal").classList.remove("show");
    }
 
    function removeRow(current) {
        current.parentElement.parentElement.remove();
    }
</script>

SHARE ON
Buffer

Enjoyed this article?

Like us on

HTML5 Javascript

Avatar for SNK

About SNK

Hello Welcome to my Blog. I develop Websites Using Laravel Framwork & WordPress. Get Latest updates on Facebook | Twitter

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Get Connected !! With Us

TOP POSTS

  • How to Setup Spring MVC Project From Scratch in Intellij IDEA ?
  • Spring Configuration Class in JAVA [Class VS XML Config]
  • Annotations Based Configuration in Spring Framework
  • How to Configure Spring Framework with XML Configurations?
  • How to Remove Project Name from URL in JSP Project in Intellij IDEA ?

TUTORIALS TILL DATE

  • September 2022 (6)
  • June 2021 (7)
  • October 2020 (5)
  • September 2020 (6)
  • September 2018 (14)
  • August 2018 (3)
  • July 2018 (4)
  • March 2018 (8)
  • February 2018 (5)
  • January 2018 (1)
  • December 2017 (2)
  • August 2017 (8)
  • May 2017 (1)
  • April 2017 (1)
  • March 2017 (4)
  • February 2017 (3)
  • January 2017 (4)

CATEGORIES

  • Angular (2)
  • CSS3 (3)
  • D3 (3)
  • HTML5 (7)
  • JAVA (11)
  • Javascript (20)
  • jQuery (8)
  • Laravel (35)
  • Others (3)
  • PHP (11)
  • Spring (2)
  • WordPress (10)

Top Categories

  • Angular
  • CSS3
  • D3
  • HTML5
  • JAVA
  • Javascript
  • jQuery
  • Laravel
  • Others
  • PHP
  • Spring
  • WordPress

Get in Touch

DMCA.com Protection Status

Recent Articles

  • How to Setup Spring MVC Project From Scratch in Intellij IDEA ?
  • Spring Configuration Class in JAVA [Class VS XML Config]
  • Annotations Based Configuration in Spring Framework
  • How to Configure Spring Framework with XML Configurations?
  • How to Remove Project Name from URL in JSP Project in Intellij IDEA ?

© 2012-22 CodeIn House.  •  All Rights Reserved.