54

My table is as follows:

<table id='demoTable'>
   <tr>
       <td>8: Tap on APN and Enter <B>www</B>.
           <INPUT id=h150000000000000109743 class=hid value="test value" type=hidden>
           <INPUT id=h250000000000000109743 class=hid1 value="26,222,98,10,50000000000000109744,T,~25,221,99,10,,T,www" type="hidden">
       </td>
   </tr>
</table>

I want to change the text only 8: Tap on APN and Enter <B>www</B>.
without affecting the hidden fields

I am trying jQuery but not finding the solution

function changeText() {
    $("#demoTable td").each(function () {
        for (var i = 0; i < $(this).children.length; i++) {
            alert($(this).children(i).val());
        }
        // alert($(this).html());
        // $(this).text("hello");
        // alert($(this).html());
    });
}
शेखर
  • 17,412
  • 13
  • 61
  • 117
  • I have the same text in another language I want to replace with that. The new text will also contain www – शेखर Dec 11 '12 at 09:43
  • Possible duplicate of [JQuery change inner text but preserve html](https://stackoverflow.com/questions/5232862/jquery-change-inner-text-but-preserve-html) – serv-inc Aug 03 '17 at 15:43

6 Answers6

76

Using text nodes in jquery is a particularly delicate endeavour and most operations are made to skip them altogether.

Instead of going through the trouble of carefully avoiding the wrong nodes, why not just wrap whatever you need to replace inside a <span> for instance:

<td><span class="replaceme">8: Tap on APN and Enter <B>www</B>.</span></td>

Then:

$('.replaceme').html('Whatever <b>HTML</b> you want here.');
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • You are right but the code is all ready running on the basis of classes on the td. So If i put inside my td will the appearance of the span will change or the td class will applied on the span tag also – शेखर Dec 11 '12 at 09:50
  • @krshekhar Any style you define on the `` will be "inherited" by the `` you put inside. – Ja͢ck Dec 11 '12 at 09:57
  • Seems like lots of operations on a td via jQuery is buggy no matter what. Using vanilla JS seems to be fine though... Anybody have a clear explanation as to why? – Ash Blue May 30 '13 at 17:54
  • @AshBlue I'm not aware of buggy behaviour. jQuery is just not well suited for working on text node, in most cases that's actually a desired trait :) – Ja͢ck May 30 '13 at 23:02
  • text how to replace text inside td using id..? – saidesh kilaru Nov 13 '13 at 13:37
5

Remove the textnode, and replace the <b> tag with whatever you need without ever touching the inputs :

$('#demoTable').find('tr > td').contents().filter(function() {
    return this.nodeType===3;
}).remove().end().end()
  .find('b').replaceWith($('<span />', {text: 'Hello Kitty'}));

FIDDLE

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • what is the meaning of this.nodeType===3. Can you explain it a bit. – शेखर Dec 11 '12 at 09:26
  • @krshekhar - Nodes have different types, a textnode that's not wrapped inside an element would have type of 3, so this gets the text that's not wrapped in an element, i.e. `8: Tap on APN and Enter ` and removes it and replaces the `` tag with a span, which can contain anything you'd like, text, html whatever. – adeneo Dec 11 '12 at 09:39
  • See I have to replace the td text with new text which will also contain www – शेखर Dec 11 '12 at 09:46
  • @undefined has an answer that does exactly that ! – adeneo Dec 11 '12 at 09:50
5
$('#demoTable td').contents().each(function() {
    if (this.nodeType === 3) {
        this.textContent
        ? this.textContent = 'The text has been '
        : this.innerText  = 'The text has been '
    } else {
        this.innerHTML = 'changed';
        return false;
    }
})

http://jsfiddle.net/YSAjU/

Ram
  • 143,282
  • 16
  • 168
  • 197
1

How about:

function changeText() {
    $("#demoTable td").each(function () {
       $(this).html().replace("8: Tap on APN and Enter <B>www</B>", "");
    }
}
webnoob
  • 15,747
  • 13
  • 83
  • 165
1

A bit late to the party, but JQuery change inner text but preserve html has at least one approach not mentioned here:

var $td = $("#demoTable td");
$td.html($td.html().replace('Tap on APN and Enter', 'new text'));

Without fixing the text, you could use (snother)[https://stackoverflow.com/a/37828788/1587329]:

var $a = $('#demoTable td');
var inner = '';
$a.children.html().each(function() {
    inner = inner + this.outerHTML;
});
$a.html('New text' + inner);
serv-inc
  • 35,772
  • 9
  • 166
  • 188
0

Wrap your to be deleted contents within a ptag, then you can do something like this:

$(function(){
  $("td").click(function(){ console.log($("td").find("p"));
    $("td").find("p").remove();    });
});

FIDDLE DEMO: http://jsfiddle.net/y3p2F/

Vivek S
  • 5,384
  • 8
  • 51
  • 72