Kanagavelu Sugumar
Kanagavelu Sugumar

Reputation: 19260

How to update HTML table cell using jquery

Team,

Could you please tell me how to update a cell using jquery?

<table id='lop'>
    <thead id='loph'>
        <tr>
            <td class='type'>Type</td>
            <td class='name'>Name</td>
        </tr>
    </thead>
    <tbody id='lopb'>
        <tr id='1'>
            <td class='type'>W</td>
            <td class='name'>ZZZ</td> 
        <tr>
        <tr id='2'>
            <td class='type'>W</td>
            <td class='name'>YYY</td> 
        <tr>        
    </tbody>
</table>

I would like to update second row name to XXX from YYY.
Please, The answer should be based on id and class name used in the html, not number/order based while using the jquery.

Also the below solution is not working,

$('#lop #2 .name').html('XXX')  //Working

in the case of

var rowID = '#2';
$('#lop rowID .name').html('XXX') //Not Working

Upvotes: 0

Views: 14319

Answers (8)

Manikandan
Manikandan

Reputation: 627

Please specify when you want to do this action of changing the cell data.

For changing, please try,

$('#2 .name').html('XXX') 

Thanks Manikandan

Upvotes: 0

Lucas Willems
Lucas Willems

Reputation: 7063

Try this code, which is using the html method to update the code :

$('#lop #2 .name').html('XXX')

You can have a look to this fiddle : http://jsfiddle.net/cWQRY/

If you want to do it with a variable, you can try this code :

var rowID = '#2';
$('#lop '+rowID+' .name').html('XXX')

Upvotes: 4

Ganesh Rengarajan
Ganesh Rengarajan

Reputation: 2006

 $('#lop').each(function(){
                var $row = $(this).parents('tr');                  
                alert($row.find('td:eq(0)').html());
                alert($row.find('td:eq(1)').html());
            });

Upvotes: 0

Koushik Rout
Koushik Rout

Reputation: 179

In Table using ID in each tr and td is not a good practice better you use index.

$('#lopb tr').filter(':eq(1) td.name').text('XXX');

Upvotes: 0

Iren Patel
Iren Patel

Reputation: 757

Try this:

* {
    font-family:Consolas
}

.editableTable {
    border:solid 1px;
    width:100%
}

.editableTable td {
    border:solid 1px;
}

.editableTable .cellEditing {
    padding: 0;
}

.editableTable .cellEditing input[type=text]{
    width:100%;
    border:0;
    background-color:rgb(255,253,210); 
}

Example: http://mrbool.com/how-to-create-an-editable-html-table-with-jquery/27425

Upvotes: 0

Goutam Pal
Goutam Pal

Reputation: 1763

$('#2 .name').html('XXX');

Check the js Fiddle: http://jsfiddle.net/Xyn9e/7/

Upvotes: 0

Manoj Yadav
Manoj Yadav

Reputation: 6612

Try this:

$('#2 .name).text('XXX');

Upvotes: 0

bipen
bipen

Reputation: 36531

make sure you don't use integers as ids use string..

 <tr id='id2'>

why:

1) It violates W3C specification.

2) It is detrimental to SEO.

3) There can be server-side scripting issues

jquery

$('#id2 .name').text('XXX')

Upvotes: 3

Related Questions