3

I am trying to replace one whole row using JQuery but seems not working ( the row is NOT being replaced). Here is the link to the example code: http://jsfiddle.net/s2kwb/

JavaScript:

$("td.99999").first().parent().next().replaceWith("<tr><td >category 3</td>
<td >2222</td>
<td >something 22</td>
<td >something 22</td>
<td >$3,433</td>
<td >$300</td>
<td >$3,733</td>
<td >$349</td>
<td >$4,082</td>
</tr>");​

Html:

<table border="1">
<tr >
  <th>category</th>
  <th>rank</th>
  <th>priority</th>
  <th>contact</th>
  <th>price</th>
  <th>tax</th>
  <th>total price</th>
  <th>shipping</th>
  <th>Net payment</th>
</tr>
<tr class="displaytagOddRow">
  <td class="99999">category 1</td>
  <td class="99999">99999</td>
  <td class="99999">something</td>
  <td class="99999">something</td>
  <td class="99999 alignRight">$3,433</td>
  <td class="99999 alignRight">$300</td>
  <td class="99999 alignRight">$3,733</td>
  <td class="99999 alignRight">$349</td>
  <td class="99999 alignRight">$4,082</td>
</tr>
<tr class="displaytagOddRow" style="Background-color:Red">
  <td class="3333">category 2</td>
  <td class="3333">3333</td>
  <td class="3333">something</td>
  <td class="3333">something</td>
  <td class="3333 alignRight">$3,433</td>
  <td class="3333 alignRight">$300</td>
  <td class="3333 alignRight">$3,733</td>
  <td class="3333 alignRight">$349</td>
  <td class="3333 alignRight">$4,082</td>
</tr>
</table>

What am I doing wrong?

Thanks in advance.

0

2 Answers 2

5

Terminate the string for every line. See also the updated jsFiddle.

$("td.99999").first().parent().next().replaceWith("<tr><td >category 3</td>" +
    "<td >2222</td>" +
    "<td >something 22</td>" +
    "<td >something 22</td>" +
    "<td >$3,433</td>" +
    "<td >$300</td>" +
    "<td >$3,733</td>" +
    "<td >$349</td>" +
    "<td >$4,082</td>" +
    "</tr>");​

The Google JavaScript Style Guide recommends this string concatenation and advises against multiline strings with the alternative backslash line breaks.

whitespace after the slash will result in tricky errors

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

Comments

2

Fixing the errors in your fiddle and adding jQuery as a reference made it work:

DEMO

Your string was not proper string literal. The console in the browser debug tool also stated that:

SyntaxError: unterminated string literal [Break On This Error]

$("td.99999").first().parent().next().replaceWith("category 3

Fixed script:

$("td.99999").first().parent().next().replaceWith("<tr><td >category 3</td><td >2222</td><td >something 22</td><td >something 22</td><td >$3,433</td><td >$300</td><td >$3,733</td><td >$349</td><td >$4,082</td></tr>");

1 Comment

This works too.thank you.Realized that the newline was the issue

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.