0

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

8 Answers 8

4

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')
Sign up to request clarification or add additional context in comments.

8 Comments

What is the benefit if i use .html than .text ??
In you case, there is no benefit. But with html method, you can edit the text and html code if there is.
Thanks!. Could you please also give me link for the syntax for $('#id1 #id2 .className), Is this only applicable to the table or can use it in some other dom structure.
Yes you can use it for some other dom structure.
This is not helping; in the case like; var rowId = '#2'; $('#lop rowId .name').html('XXX')
|
3

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')

Comments

0

Try this:

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

Comments

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

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

Comments

0
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

Comments

0

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');

Comments

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

Comments

0

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

For changing, please try,

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

Thanks Manikandan

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.