0

I have a situation where I have a HTML table.
It has some td.
Every td has some text and two hidden field.
I have a json source for my table td text.
I want to change the text of the all the td of the table without changing the two hidden field.
Earlier I was using jTemplate and json for the same.
But I was bringing the hidden field values in the Jason for every td which was not different from the original value.
So I decided to change my approach to bring only the text which need to be replace.
I know how to change the text of the td in jQuery using text() and html() method.
But will it change the text without affecting the controls (hidden field) inside td?

---edit ---
One of my td is like

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

And my jquery is like this

 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

2 Answers2

1

you can simply do this

get your table td and change the first child content like this if you have first child as text content

var text_to_chang = document.getElementById("td").childNodes[0];
text_to_change.nodeValue = 'new text';
Murali N
  • 3,451
  • 4
  • 27
  • 38
  • do everything inside create a childNodes. If yes then will I have three childNodes in my td? – शेखर Dec 11 '12 at 07:05
  • place an alert and test for childnodes like alert(td.childNodes.length) and tell me how many childNodes you have – Murali N Dec 11 '12 at 07:10
  • what about the white spaces in my text of td will it create a childNode? Please see this link http://stackoverflow.com/questions/12635751/jquery-equivalent-browser-compatibility-for-e-parentnode-parentnode-childnodes – शेखर Dec 11 '12 at 07:10
  • text inside a td is considered as text node – Murali N Dec 11 '12 at 07:12
0

Put the text in a label, and then use text() on the label instead of the td for example:

<td>
   <label id="lblName">krshekrar</label>
   <input type="hidden" value="1" id="UserID"/>
</td>
robasta
  • 4,621
  • 5
  • 35
  • 53