0

I have this:

<td class="cant_formula numeric">
    0.007
    <input type="hidden" value="0.015" class="cant_formula"/>
</td>

I want to replace the text inside it, I tried these ways:

$('td.cant_formula.numeric').contents().first()[0].textContent='fsdfwffwwfwf';

but it doest not work!!

Here is the fiddle: https://jsfiddle.net/gp5gmmgx/

Can you help me, What Im doing wrong??

juanpscotto
  • 990
  • 1
  • 13
  • 32
  • 1
    possible duplicate of [How do I select text nodes with jQuery?](http://stackoverflow.com/questions/298750/how-do-i-select-text-nodes-with-jquery) – Sterling Archer Mar 23 '15 at 21:08

3 Answers3

1

Look at this https://jsfiddle.net/gp5gmmgx/2/

$('td.cant_formula.numeric').contents()
    .filter(function() {
        return this.nodeType == 3; //Node.TEXT_NODE
    })
    .get(0).nodeValue = 'test';
  1. table element is required else td elements aren't selected;
  2. about selecting text nodes read https://stackoverflow.com/a/298758/3349900.
Community
  • 1
  • 1
tutankhamun
  • 880
  • 2
  • 11
  • 21
0

jsFiddle doesn't seem to work with that version of jQuery. (I can't even get a simple alert to work.)

Upgrade to at least v 1.9.1

Also, td elements must be within tr elements, which must be within a table.

https://jsfiddle.net/0rnLa9j1/

Rick Hitchcock
  • 35,202
  • 5
  • 48
  • 79
-1

Try

$('td.cant_formula.numeric').text("text");

Actually after testing, this replaces all the contents of the td not just the text. I should have listened to my gut and double checked before answering, apologies!

John C
  • 3,052
  • 3
  • 34
  • 47