Milos Cuculovic
Milos Cuculovic

Reputation: 20223

Add or edit an element in the HTML table with JQuery

I have a HTML table with some elements inside like that:

<table width='700px' id="employeetable" class="tablesorter" style='table-layout:fixed;'>
<thead>
    <tr>
        <th>Select to Edit</th>
        <th>Group Id</th>
        <th>User Id</th>
        <th>Object</th>
        <th>Read</th>
        <th>Update</th>
        <th>Insert</th>
        <th>Delete</th>
    </tr>
</thead>
<tbody>
    {foreach from=$privileges item=privilegesItem name=foo}
    <tr>
        <td align="center"><input id='{$smarty.foreach.foo.iteration}' type="radio" name="SelcetedObject" value= "{$privilegesItem['Object']}"/> </td>
        <td align="center">{$privilegesItem['group_id']}</td>
        <td align="center">{$privilegesItem['user_id']}</td>
        <td align="center">{$privilegesItem['Object']}</td>
        <td id='read_{$smarty.foreach.foo.iteration}'   align="center">{$privilegesItem['Read']}  </td>
        <td id='update_{$smarty.foreach.foo.iteration}' align="center">{$privilegesItem['Update']}</td>
        <td id='insert_{$smarty.foreach.foo.iteration}' align="center">{$privilegesItem['Insert']}</td>
        <td id='delete_{$smarty.foreach.foo.iteration}' align="center">{$privilegesItem['Delete']}</td>
    </tr>
    {/foreach}
</tbody>

I am now able to delete an element with

$("input[type='radio']:checked").each(function () {
        var id = this.id;
        var id_read = '#read_'+id;

        $(id_read).remove();

But I would like to edit the lement from the table and not to delete it. Edit: If I have for example value 10 for my Read item, I would like to chenge it to 2, or instead of having an simple value, I would like to replace it with an checkbox.

How could I do that please? With .add ?

Upvotes: 2

Views: 1552

Answers (2)

AbstractChaos
AbstractChaos

Reputation: 4211

A editable table sounds kinda what you mean. below would replace your data with a input to edit and then back again.

$(document).ready(function() {
    $(".tablesorter td").not(':first').on('click',null,function() {
       var data = $(this).html();
        if(!($(this).children('input').length > 0)) {
            $(this).html("<input type='text' id='focus' value='"+data+"' />").children().focus();
        }
    });

    $(".tablesorter td").on('blur','#focus',function() {
       $(this).parent().html($(this).val());
    });
});​

fiddle

Upvotes: 0

omerkirk
omerkirk

Reputation: 2527

If you want to change the text inside td element you can just do

$(id_read).text("Permitted") //Forbidden
$(id_read).css() //change its style

I could be of better help if you explain what you mean by "Edit"

Upvotes: 1

Related Questions