0

I have a button on my web page that, when clicked, does the following:

  1. Gets the contents of a "template table" by calling .html() on that table
  2. Stores the contents of the above call in a variable
  3. Does a global replace on that variable to track table row number
  4. Replaced contents are added to my "real table"

Problem: Values entered by users into input fields in those table rows are not preserved. Here's the code:

function addRowToCluesTable() {
    var t = $("#template").html().replace(/{NUM}/g, stepCount);
    var r = $("#real").html();
    $("#real").html(r + "\n\n" + t);
}

This all works fine EXCEPT that any input fields in the "real" table (non-template) containing values entered by users are lost and all values are returned to their default or empty state. So the flow goes like this: row is added, values entered into real table, another row added, values entered...repeat.

How can I get jquery to preserve the user-supplied input values when dynamically adding rows? Instead of storing real table contents in a variable should I instead append template rows to the end of the real table? If so, what's the best way to do that?

1 Answer 1

1

Try appending the new html instead:

$('#real').append(t);
Sign up to request clarification or add additional context in comments.

Comments

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.