1

I have following table

<table class="data">
    <thead>
    <tr>
        <th>1</th>
        <th>2</th>
        <th>3</th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td>
            1 data
        </td>
        <td>
            2 data
        </td>
        <td>
            123456789123
        </td>
    </tr>
    </tbody>
</table>

how can I dynamically scan table and replace only values in third table body td values where information like 123456789123 is stored.

This information should be placed with certain character on certain string location so

<td> 123456789123 </td> should be <td> 12345678*12* </td>

4 Answers 4

1

Please find below code block for your need, I have added one specific class to TD for which you want to modify value.

$( document ).ready(function() {
  $('.value_td').each(function(key, ele){
      
    // Getting Original Value
    var original_val = $(ele).text().trim();
    
    // You can change your logic here to modify text
      var new_value = original_val.substr(0, 8) + '*' + original_val.substr(9, 2) + '*';
      
    // Replacing new value
    $(ele).text(new_value);
  });
});
<table class="data">
    <thead>
    <tr>
        <th>1</th>
        <th>2</th>
        <th>3</th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td>
            1 data
        </td>
        <td>
            2 data
        </td>
        <td class="value_td">
            123456789123
        </td>
    </tr>
    </tbody>
</table>

JS Fiddle

Sign up to request clarification or add additional context in comments.

2 Comments

great, how can I change var new_value = original_val+'edited'; to match pattern to change last character end one before last to * character,
@user1765862 See my answer, with that you can replace the position that you want.
1

To replace the selected texts by indexs, use this:

// replace the 'n'th character of 's' with 't'
function replaceAt(s, n, t) {
    return s.substring(0, n) + t + s.substring(n + 1);
}

$('td:nth-of-type(3)').each(function(i, item){
    $(this).text($(this).text().trim()); // remove extra spaces
    $(this).text(replaceAt($(this).text(), 8, '*')); // replace character in position 8
    $(this).text(replaceAt($(this).text(), 11, '*')); // replace character in position 11
});

See the working demo: https://jsfiddle.net/lmgonzalves/6ppo0xp3/

Comments

0

Try this:

$('.data td:nth-child(3n)').text('foo');

This will change every 3rd td’s text inside .data to foo. Here’s a demo: http://jsbin.com/katuwumeyu/1/edit?html,js,output

Let me know if that helps, I’ll gladly adapt my answer in case this isn’t what you need.

2 Comments

thanks, I want not last row but each row with this(any) third td content.
it has dynamic content but that content should be replaced with * on those string locations.
0

You can use jquery ":eq(2)" to track 3rd td position like this:

var el = $('table.data tbody tr td:eq(2)');
el.text(el.text().replace('123456789123','12345678*12*'));

https://jsfiddle.net/v25gu3xk/

or maybe you need to replace char positions:

var el = $('table.data tbody tr td:eq(2)');
var vl = el.text().trim();
el.text(vl.substr(0, 8) + '*' + vl.substr(9, 2) + '*');

https://jsfiddle.net/v25gu3xk/1/

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.