views:

377

answers:

4

I have a table with a few rows...

I select a row and click on modify and I should be able to make all the cell of that row editable...

how does one do to make a cell editable in javascript?

Is it better to use Jquery and how ?

+2  A: 

You can insert textboxes inside each cell and set the values to be that of the table cell.

Something like this

$(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>

You can then place an update button and fetch the values from the textboxes and update.

rahul
+3  A: 

Here's a quick concept I just worked up for you:

$(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>
Jonathan Sampson
He does technically state that he wants to click some kind of 'modify' button, but imho your way is better. yellow-05, what this function does is turns any table cells you double-click into text-areas, so you'll need to expand on it a little to make it do the whole row
Mala
@Mala, I've updated the code a bit.
Jonathan Sampson
A: 

Setcontent-editable property of each element you want to edit.
http://html5demos.com/contenteditable

alemjerus
+2  A: 

There's no need to do your own code, a plugin for jQuery for this very purpose exists already. Try jEditable, it can do exactly what you need.

Their demo page has some nice examples:

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

Tatu Ulmanen
First they want their religion recognised, now these Jedi want tables all to themselves! Where will it end?
Matt Ellen