Zero Error Prototype Replacement Method

I'm new to the prototype, but pretty experienced with jQuery, so I just don't understand how the prototype works. I am trying to do the following

$$("tr.total_line td.total_cell").first().replace(newTotal);

but i get TypeError: Cannot read property 'firstChild' of null

When I execute $$("tr.total_line td.total_cell").first()in the JS console, I get the result of the DOM element.

Here is the corresponding markup

<tr class="total_line">
    <td colspan="2">Total</td>
    <td class="total_cell">$50.00</td>
    <td></td>
</tr>
+3
source share
2 answers

Since it .first()returns an element <td>, you need to insert <td>or <th>for the replacement to be valid HTML.

So if newTotal:

"<td>$100</td>"

... it should work. But if it is simple:

"$100"

... this is not true.

Or another option is to replace innerHTML <td>:

$$("tr.total_line td.total_cell").first().innerHTML = newTotal;
+2
source

replace , update, .

0

Source: https://habr.com/ru/post/1787871/


All Articles