Create Editable HTML table with source code

This post shows how to create an editable table in pure JavaScript and using the jQuery plugin. There are various features that you can add to the editable table, for example;

  • You can create a table to add/delete rows or columns
  • You can create an editable cell in the table
  • You can change the color of the cell on click
  • Hide row/column on mouse click
  • Get the content of the cell/row/column on mouse click
  • Adding checkboxes in the table

Let’s start and see some of these in working live demos

Editable cell in HTML table Using jquery

This jQuery code will make the cell editable on double-click. To use the code, simply add class to the table cell and put the above jQuery in the head.

$(function () {
    $("td").dblclick(function () {
        var OriginalContent = $(this).text();

" + OriginalContent + "

        $(this).children().first().keypress(function (e) {
            if (e.which == 13) {
                var newContent = $(this).val();



Editable Cell in HTML table using Ajax

$(this).children().first().keypress(function (e) {
	if (e.which == 13) {
		var fullContent = $(this).val();
	        //Call the server side file to communicate with database.
	        var URL = '/callfile.php' ;
	        //alert (URL);
	        var xmlhttp;    
	        if (window.XMLHttpRequest){
                        // code for IE7+, Firefox, Chrome, Opera, Safari
	        	xmlhttp=new XMLHttpRequest();
                        // code for IE6, IE5
	        	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                       // if successfully done it will return status 200
	        	if (xmlhttp.readyState==4 && xmlhttp.status!=200){
	        		alert ('Can not update comment. Contact admin')


Add/Delete rows in HTML table using Javascript

If you want to add or delete a row in the table on click you can use append() functions.

$('button').live('click' , function(event){
      var $row = (" 6  class6  school6  ");

There are scenarios where we have the whole row from one table to another. This example will show how you can add a row in first table and remove it from second table.

$('#example tbody tr td').live('click' , function(event){
      var $row = $(this).closest('tr');
      $(this).closest("tr").remove(); // remove row

$('#example2 tbody tr td').live('click' , function(event){
      var $row = $(this).closest('tr');
      $(this).closest("tr").remove(); // remove row

Javascript to get table content in the form on mouse click

If you need to get all table contents in the form to edit, you can use the below javascript code.

  $('#htmlgrid tbody tr td').on('click', 'a', function(event){
    var first_cell = $('td:eq(0)', $(this).parent().parent()).text();
    var second_cell = $('td:eq(1)', $(this).parent().parent()).text();
    var third_cell = $('td:eq(2)', $(this).parent().parent()).text();

    var answer = prompt('Please enter your Password', '');
    if (answer!=null){
      password = $.trim(answer);
      password_from_db = password_from_db()
        document.forms[0].elements['first_cell'].value = first_cell;
        document.forms[0].elements['second_cell'].value = second_cell;
        document.forms[0].elements['third_cell'].value = third_cell;

Hiding table row using jQuery

There are many ways to hide table rows. You can either use jQuery or CSS.
This is jQuery code that will hide the first and second th and td of the table.

<$(document).ready(function() {

To hide table row using CSS you can use "display: none"

Single checkbox functionality for table rows.

$('table tr').click(function() {

 checkBox = $(this).children('td').children('input[type=checkbox]');

  checkBox.attr('checked', '');
  checkBox.attr('checked', 'checked');

check/uncheck all checkbox in HTML table

$('.check-all').click(function() {

 checkBox = $('table tr').children('td').children('input[type=checkbox]');

  checkBox.attr('checked', 'checked');
  checkBox.attr('checked', ''); 

Drag and Drop rows from one table to another

This example shows the drag and drop of one row from the first table to the second table.

CSS code for drag and drop in HTML table

ul li {
    min-width: 200px;
.dragging li.ui-state-hover {
    min-width: 240px;
.dragging .ui-state-hover a {
    color:green !important;
    font-weight: bold;
th,td {
    text-align: right;
    padding: 2px 4px;
    border: 1px solid;
.connectedSortable tr, .ui-sortable-helper {
    cursor: move;
.connectedSortable tr:first-child {
    cursor: default;
.ui-sortable-placeholder {
    background: yellow;

jQuery code for Drag and Drop HTML table

$(document).ready(function() {
    $tabs = $(".tabbable");

    $('.nav-tabs a').click(function(e) {

    $( "tbody.connectedSortable" )
            connectWith: ".connectedSortable",
            items: "> tr:not(:first)",
            appendTo: $tabs,
            zIndex: 999990,
            start: function(){ $tabs.addClass("dragging") },
            stop: function(){ $tabs.removeClass("dragging") }

    var $tab_items = $( ".nav-tabs > li", $tabs ).droppable({
      accept: ".connectedSortable tr",
      hoverClass: "ui-state-hover",
      over: function( event, ui ) {
        var $item = $( this );

      drop: function( event, ui ) {
        return false;



3 thoughts on “Create Editable HTML table with source code”

  1. Shabina Siddique

    The first example code is to create editable cell in tale. Try using that, let me know if you have difficulties implementing the code

  2. I’m not understanding the “cellEditing” what is that where to add that class in HTML

Comments are closed.