In our last article, we discussed CRUD operations using JSON in JavaScript and in this article, We will discuss How to edit/delete the selected row from the HTML table using JavaScript with icons.
we are using the Bootstrap table and I have placed two action buttons icons in the table i.e edit and delete. Now onClick edit button, we convert selected row into an editable format so that users can update their information and the delete button should remove the row.
we going to provide an easy way to make them editable inline so let’s make the edit and delete buttons functional.
How to edit selected row from HTML table using JavaScript
<!DOCTYPE html>
<html>
<head>
<title>How to edit/remove selected row from HTML table using JavaScript</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />
</head>
<body>
<div class="container">
<h1>How to edit/delete selected row from HTML table using JavaScript</h1>
<br />
<fieldset>
<legend>
Customer List
</legend>
<table class="table">
<thead>
<tr>
<th>CustomerId</th>
<th>CustomerName</th>
<th>CustomerEmail</th>
<th>Address</th>
</tr>
</thead>
<tbody id="tblbody">
<tr id="119447">
<td class="td-data">119447</td>
<td class="td-data">Qa</td>
<td class="td-data">anny@gmail.com</td>
<td class="td-data">USA</td>
<td class="td-data">
<button class="btn btn-info btn-xs btn-editcustomer" onclick="showEditRow()">edit</button>
<button class="btn btn-danger btn-xs btn-deleteCustomer" onclick="deleteRow()">delete</button>
</td>
</tr>
<tr id="119443">
<td class="td-data">119443</td>
<td class="td-data">Qa</td>
<td class="td-data">kill@gmail.com</td>
<td class="td-data">USA</td>
<td class="td-data">
<button class="btn btn-info btn-xs btn-editcustomer" onclick="showEditRow()">edit</button>
<button class="btn btn-danger btn-xs btn-deleteCustomer" onclick="deleteRow()">delete</button>
</td>
</tr>
<tr id="119437">
<td class="td-data">119437</td>
<td class="td-data">Qa</td>
<td class="td-data">lamind@gmail.com</td>
<td class="td-data">USA</td>
<td class="td-data">
<button class="btn btn-info btn-xs btn-editcustomer" onclick="showEditRow()">edit</button>
<button class="btn btn-danger btn-xs btn-deleteCustomer" onclick="deleteRow()">delete</button>
</td>
</tr>
<tr id="119428">
<td class="td-data">119428</td>
<td class="td-data">Maduwantha</td>
<td class="td-data">anyy@gmail.com</td>
<td class="td-data">CAN</td>
<td class="td-data">
<button class="btn btn-info btn-xs btn-editcustomer" onclick="showEditRow()">edit</button>
<button class="btn btn-danger btn-xs btn-deleteCustomer" onclick="deleteRow()">delete</button>
</td>
</tr>
<tr id="119422">
<td class="td-data">119422</td>
<td class="td-data">kamii</td>
<td class="td-data">kamii@gmail.com</td>
<td class="td-data">usa</td>
<td class="td-data">
<button class="btn btn-info btn-xs btn-editcustomer" onclick="showEditRow()">edit</button>
<button class="btn btn-danger btn-xs btn-deleteCustomer" onclick="deleteRow()">delete</button>
</td>
</tr>
<tr id="119421">
<td class="td-data">119421</td>
<td class="td-data">kellu</td>
<td class="td-data">kellu@gmail.com</td>
<td class="td-data">usa</td>
<td class="td-data">
<button class="btn btn-info btn-xs btn-editcustomer" onclick="showEditRow()">edit</button>
<button class="btn btn-danger btn-xs btn-deleteCustomer" onclick="deleteRow()">delete</button>
</td>
</tr>
<tr id="119411">
<td class="td-data">119411</td>
<td class="td-data">damna</td>
<td class="td-data">damna@gmail.com</td>
<td class="td-data">usa</td>
<td class="td-data">
<button class="btn btn-info btn-xs btn-editcustomer" onclick="showEditRow()">edit</button>
<button class="btn btn-danger btn-xs btn-deleteCustomer" onclick="deleteRow()">delete</button>
</td>
</tr>
<tr id="119409">
<td class="td-data">119409</td>
<td class="td-data">csd</td>
<td class="td-data">tinku@gmail.com</td>
<td class="td-data">France</td>
<td class="td-data">
<button class="btn btn-info btn-xs btn-editcustomer" onclick="showEditRow()">edit</button>
<button class="btn btn-danger btn-xs btn-deleteCustomer" onclick="deleteRow()">delete</button>
</td>
</tr>
<tr id="119385">
<td class="td-data">119385</td>
<td class="td-data">asd</td>
<td class="td-data">tork@gmail.com</td>
<td class="td-data">Paris</td>
<td class="td-data">
<button class="btn btn-info btn-xs btn-editcustomer" onclick="showEditRow()">edit</button>
<button class="btn btn-danger btn-xs btn-deleteCustomer" onclick="deleteRow()">delete</button>
</td>
</tr>
</tbody>
</table>
</fieldset>
</div>
</body>
</html>
<script type="text/javascript">
function showEditRow() {
debugger;
var rowdataId = event.target.parentNode.parentNode.id;
//this gives id of tr whose button was clicked
/*returns array of all elements with "td-data"" class within the row with given id*/
var data = document.getElementById(rowdataId).querySelectorAll(".td-data");
var ID = data[0].innerHTML;
var name = data[1].innerHTML;
var email = data[2].innerHTML;
var address = data[3].innerHTML;
data[0].innerHTML = '<input name="txtupdate_ID" disabled id="txtupdate_ID" value="' + ID + '"/>';
data[1].innerHTML = '<input name="txtupdate_Name" id="txtupdate_Name" value="' + name + '"/>';
data[2].innerHTML = '<input name="txtupdate_email" id="txtupdate_email" value="' + email + '"/>';
data[3].innerHTML = '<input name="txtupdate_Address" id="txtupdate_Address" value="' + address + '"/>';
data[4].innerHTML =
"<button class='btn btn-primary btn-xs btn-updateCustomer' onclick='updateData()'>" +
"Update</button>"
+ "<button class='btn btn-warning btn-xs btn-cancelupdate' onclick='cancelUpdate()'>Cancel</button>"
+ "<button class='btn btn-danger btn-xs btn-deleteCustomer' onclick='deleteRow()'>"
+ "Delete</button>"
}
function cancelUpdate() {
var rowdataId = event.target.parentNode.parentNode.id;
var dataRow = document.getElementById(rowdataId); //this gives tr of whose button was clicked
var trRow = dataRow.querySelectorAll(".td-data");
var Name = trRow[1].querySelectorAll("#txtupdate_Name")[0].value;
var email = trRow[2].querySelectorAll("#txtupdate_email")[0].value;
var address = trRow[3].querySelectorAll("#txtupdate_Address")[0].value;
trRow[0].innerHTML = rowdataId;
trRow[1].innerHTML = Name;
trRow[2].innerHTML = email;
trRow[3].innerHTML = address;
var actionbtn = "<button class='btn btn-info btn-xs' onclick='showEditRow()'>Edit</button>" +
"<button class='btn btn-danger btn-xs' onclick='deleteRow()'>Delete</button>"
trRow[4].innerHTML = actionbtn;
}
function deleteRow() {
var rowdataId = event.target.parentNode.parentNode.id;
document.getElementById(rowdataId).remove();
}
function updateData() {
var rowdataId = event.target.parentNode.parentNode.id;
var dataRow = document.getElementById(rowdataId); //this gives tr of whose button was clicked
var datatr = dataRow.querySelectorAll(".td-data");
var Name = datatr[1].querySelector("#txtupdate_Name").value;
var email = datatr[2].querySelector("#txtupdate_email").value;
var address = datatr[3].querySelector("#txtupdate_Address").value;
datatr[0].innerHTML = rowdataId;
datatr[1].innerHTML = Name;
datatr[2].innerHTML = email;
datatr[3].innerHTML = address;
var actionbtn = "<button class='btn btn-info btn-xs' onclick='showEditRow()'>edit</button>" +
"<button class='btn btn-danger btn-xs' onclick='deleteRow()'>delete</button>"
datatr[4].innerHTML = actionbtn;
}
</script>
If you have any queries or doubts, please comment.
Note*
The first and most important thing is the target audience, that is, the visitors to your website, when you come to know about the audience of your website, then your work becomes very easy.
With this, you get to understand what type of content your audience is like, now according to this you will be able to design your site further.
For example, if you are making a website for an automobile, then it is obvious that in the audience group there will be only those people who like bikes, cars, etc. What kind of information do you want from the site, now you will put only those things on your site which are useful for the person who likes the car.
The structure or structure of the website is called its layout, while designing the layout, keeping in mind the height-width, position, etc of different sections of the website like header, sidebar, content, footer, etc., the structure is designed in such away. That we are able to present the information of our site to the user in the right way.
Whenever we design our website, we also have to take care of the color combination, you must have seen that a maximum of 3 or 4 colors are used in most of the websites so that the site looks more professional.
The color of the images, fonts, backgrounds, etc. to be used is selected according to the pre-decided color combination and the same combination is used in all the pages.
Nowadays it has become very easy to create a website, there are many such tools on the internet, with the help of which you can easily create your website without any programming.
But if you want to become a professional web designer, you want to design any kind of website, and you can understand not only the design but also all the functionality inside it, then for this, you have some programming skills. It is necessary to have
The post How to leave just points without lines in ChartJS appeared first on Software Development | Programming Tutorials.
Read More Articles
- [Simple Way]-Cascading DropDownList in Asp.Net Mvc Using Jquery Ajax
- jQuery Ajax GET Example with Parameters
- How to Pass Parameters in AJAX POST?| JQuery Ajax Post Json Example
- [Simple Way]-How to get data from database using JQuery Ajax in asp net MVC
- How to add, edit and delete rows of an HTML table with Jquery?
- Registration form with image upload in MVC using jquery Ajax
- How to make an Inline editable table in MVC using jquery?
- Insert Update Delete Using Jquery Ajax and Modal Popup in Mvc
- [Solved]-How to Upload pdf file using jquery MVC?
- Dynamically creating graphs with jQuery
- Displaying a hidden text box when a particular option value selected
- Re-render CSS after JS changes the HTML element's position
- Is it possible to automatically show default jquery mobile ajax loader when I call $.post()?
- Cannot merge JSON in JS
- CustomValidator in C# not picking up colour
- Jquery search - What computes faster?
- One click behind
- Display Confirmation bootstrap Modal when ModelState is valid using an ajax POST from Razor View
- pass values from separate divs into a javascript function
- Unserialize data from HTML inside JavaScript with pure JS or jQuery
- How to change the icon of an accordion immediately after onclick on it
- How to change style of div #id that contain selected .class
- jQuery to match HTML grab product ID
- Multidimensional array not being POSTed
- text field that only allow and number and dash and also accepts ctrl commands
- How to call showTab using JQuery in LightSwitch
- jQuery Button Toggle Same Button In Different Areas of the page
- Applying class to jquery ui dialog
- Keep a bootstrap modal open, from code behind
- jQuery slideToggle not working in Internet Explorer 8
- How can you tell the difference between a generated and a selected element?
- using Switch with dropdown in jQuery, Not working
- Don't Display Photo if Not Found
- dynamic query string in jquery
- Jquery: Refresh html element which was added through jquery
- How we have multiple return statement in below code ? can any one explain how its working here?
- jQuery css fixed left but absolute top
- How to add item from select dropdown - keen-ui
- jQuery UI Droppable: Detect which draggables are dropped on an element?
- jQuery slideToggle(); bootstrap 3 not smooth animation
- JQGrid Add New Record
- How do I set a character limit in sweetalert?
- PhoneGap Ajax Call
- Taking Text from a modal input field and placing it into a table
- jQuery .css() not functioning in Safari
- Auto Focus behavior when using multible input elements within same <label> on FireFox only
- I cannot get my jquery tabs to call any of the .tab elements
- How can I display a Bootstrap popover with an html content from a GridView?
- Page is damaged during page loading process
- Iterating through mutiple checkbox values and matching them against a single text input box's value with jQuery
- Rails json parse
- Chaining function, stop and continue
- Uncaught TypeError: $(...).jstree(...).hide_all is not a function
- How to make jquery detect html elements inside a certain element?
- jqvmap - javascript custom code using change url with onregionclick
- Jquery to get Friday's Date of Current Week from Current Date
- How to get select box name in jquery
- How to filter json object from some objects. (Select only some values)
- How to apply CSS for Ajax.ActionLink
- Why would a website behave strange in live environment?
- What is the solution for Highcharts hover
- How to access "next" div in row of elements
- Full calendar extend cells
- excel button is not showing in datatables
- $(“#request-brochure”).validate is not a function
- How can I execute a function passed as an option in a Jquery plugin?
- Order of error messages to reflect the order of input fields in Jquery validation
- Show an Anchor tag in an ASP.Net page with JQuery
- jQuery $.get sometimes works, sometimes not?
- beforeunload event on page refresh in firefox
- selected item not showing up in jquery mobile select - nativedroid
- jQuery show/hide: form elements
- How to use jQuery object for a select box
- fancyBox options have no effect whatsoever
- How to implement jquery-throttle-debounce on addEventListener
- Checkbox change is being detected twice
- jQuery customize fancybox with ajax call
- PHP Ajax return specified data from my php file
- Adding audio to my page
- Autocompleting the Json
- Submitting into database, but through AJAX returns blank
- Html list expand hover min-height
- jQuery - add element inside element but not wrap the text
- when loaded image is bigger than div
- Load fancybox content inside div?
- Video starts when current slide // Superslides
- How to improve OpenLayer Vector layers refresh for 200 elements?
- display navbar by default if page cannot scroll
- select rows using ctrl key using knockout and jquery
- Event Listener is not responding
- Pass custom collection object from Controller to View with JQuery
- checking if jquery hide has completed execution
- <a> tag inside an <a> tag?
- Is there a better way to this Javascript code
- Use data from Underscore.js with a Flot.js stacked bar graph
- Change background colors with smoothState.js
- Place jQuery created html into specific div
- how to get the checkbox selected row values from Kendo Grid UI while pagination in MVC using Jquery
- jquery submit() ignores changes to input tags
- jquery my else if part not working... what is wrong?
- Take value, update field with output
- The div won't hide Javascript/HTML
- How can I implement an array list to this html code?
- How to create an array of Json objects from multiple Get Requests in Angular
- Dollar sign in the middle of JavaScript objects
- GeoCoding Google Maps Undefined
- jQuery Hold Element's Classes in Static Variable and Reuse
- Where does the 'response' come from in the JavaScript sample code for Google Drive API?
- Change css class atributes dynamically from Jquery?
- Change checkbox in jquery not working in text! template in backbone.js
- Can I set Gravity Form field on required status in Javascript function after click on checkbox?
- How to prevent click action on children element?
- A Choose preference by dragging input field?
- select second input on new dynamic added tablerow
- jQuery can't find options appended to list
- Change CSS of div when ATBar button is clicked
- jQuery & Canvas Image Crop - Index or size is negative or greater than the allowed amount
- Calculate sum of values on text-boxes based on checkbox checked
- Not all Ajax content loading on one domain
- Not able to calculate no of div if dynamically created upon call of ajax
- Concurrent Ajax Calls?
- How do I find the browser scroll bars total scrollable height
- Stop jQuery from Reloading Page when Using Multiselect
- Handling unknown number of form values
- misunderstood Javascript functions call
- jQuery MouseOut Delay Exception
- Create a function CheckDidget
- Sequence several functions on jQuery
- How Can i return true or false from this JSON Anonymous Function
- Knockout JS : Template binding Issue in IE8
- Retrieve date fullCalenadar
- Unable to get property 'apply' of undefined or null reference - IE11 and Chrome
- Click in jQuery UI dialog box triggerd multiple times
- Bootstrap datepicker - two datepickers updating each others start/end dates
- Jquery .click for one second
- Start date End date validation in angularjs submit form should be disabled
- SPFx Modal open with a regular html button in datatables.net
- Handling a create action with Rails 4
- Make jQuery to discard an action for a particular div class
- How to handle loading error in jquery Tab?
- Auto show a 'div' after hide another 'div' in javascript
- jQuery library doesn't load
- I'm looking for a jquery form validation plugin, which can give validation error as a "Alert"
- Add a background image to a connector in jsPlumb
- Using require js getting undefined for all modules other than jquery
- Get one element to scroll for multiple elements
- ContentFlow (coverflow) - Need to make it work + Tabs and panes
- last appended option in select tag always becomes the value of it
- JQuery getJSON return object array from function?
- Scrollbar on top of DIV and nowrap container
- image click replaces image with iframe
- using CSS hover with jQuery (syntax)
- Regex match function in Javascript
- scrollHeight for readonly textarea
- Using Form to Input Google Maps Data
- Getting value of a <li> tag to TextField
- Mediaelement.js hide controls after rewind on IE
- detect when pressing the deleted key using jquery
- get the value of input type text in js
- Save text of textarea in db