Editing an HTML table cell

I have a table with several rows ... I want to be able to select a row and click on the modification, and I should be able to make all the cells of this row editable ...

How to make a cell editable in Javascript? And is it better to use jQuery?

+5
source share
4 answers

There is no need to make your own code, there is now a jQuery plugin for this purpose. Try jEditable , it can do exactly what you need.

Their demo page has some nice examples:

http://www.appelsiini.net/projects/jeditable/default.html

+11

:

$(function(){
  $("button[name='doModify']").click(function(){
    // disable out modify button
    $(this).attr("disabled","disabled");
    // enable our save button
    $("button[name='save']").removeAttr("disabled");
    // cycle through each row having marked for modification
    $(":checkbox[name='modify']:checked").each(function(){
      $(this).closest("tr").find("td:gt(0)").each(function(){
        // convert each cell into an editable region
        $(this).wrapInner("<textarea name='"+$(this).attr("rel")+"'></textarea>");
      });
    });
  });
});

-

<table border="1" cellspacing="1" cellpadding="5">
  <tbody>
    <tr>
      <td><input type="checkbox" name="modify" /></td>
      <td rel="username[]">jon.doe</td>
      <td rel="information[]">This is my bio.</td>
    </tr>
    <tr>
      <td><input type="checkbox" name="modify" /></td>
      <td rel="username[]">jonathan.sampson</td>
      <td rel="information[]">This is my bio.</td>
    </tr>
    <tr>
      <td><input type="checkbox" name="modify" /></td>
      <td rel="username[]">yellow.05</td>
      <td rel="information[]">This is my bio.</td>
    </tr>
    <tr>
      <td colspan="3" align="right">
        <button name="doModify">Modify</button> 
        <button name="save" disabled="disabled">Save</button>
      </td>
    </tr>
  </tbody>
</table>
+4

Set the content-editableproperty of each item that you want to change.
http://html5demos.com/contenteditable

+4
source

You can insert text fields inside each cell and set values ​​that have table cell values.

Something like that

$(function(){
        $("#tbl1 tr").click ( function(){
            if ( !$(this).hasClass('clicked') )
            {
                $(this).children('td').each ( function() {
                    var cellValue = $(this).text();
                    $(this).text('').append ( "<input type='text' value='" + cellValue + "' />" );
                });

                $(this).addClass('clicked');
            }
        });
    });
<table id="tbl1">
            <tr>
                <td>1</td>
                <td>4</td>
                <td>3</td>
            </tr>
            <tr>
                <td>4</td>
                <td>5</td>
                <td>6</td>
            </tr>
        </table>

Then you can put the refresh button and select the values ​​from the text fields and update.

+3
source

All Articles