0

I want to add table rows dynamically using jQuery/Javascript and I have used the following code but it's not working in Chrome. Any help?

function AddTextBoxes(label,parameter, paraId) {
  strCode += "<tr><td>";
  strCode += "<label id='" + label + "1'>" + label + "</label></td><td>";
  strCode += "<input type='text' id=" + paraId +" value="+parameter.minValue + "-" + parameter.maxValue+ ">";
  $("#" + paraId).attr('value', parameter.minValue + "-" + parameter.maxValue);
  $("#" + paraId).attr('text', parameter.minValue + "-" + parameter.maxValue);
  strCode += "</td></tr>";
}
3
  • Have you declared var strCode = '' outside of the function? Commented Jul 20, 2013 at 10:13
  • 2
    You need to append the contents of strcode. Commented Jul 20, 2013 at 10:13
  • @Geek This function only creates a string with some HTML in it, what exactly means "it doesn't work" in Chrome? Commented Jul 24, 2013 at 7:22

4 Answers 4

1

You need to append the contents of strcode which you are not doing in your function.

Also:-

You need to add var strCode = '' before this line(At the beginning of the function):-

strCode += "<tr><td>";
Sign up to request clarification or add additional context in comments.

1 Comment

Actually my code is working fine in IE but not in chrome or Mozilla.I have added strCode to my div using jquery.
0

Here is the code with needed modifications. Also, you need to do somethign with the resulting string. For example append it to body. (untested)

function AddTextBoxes(label,parameter, paraId) {
  var strCode = "<tr><td>";
  strCode += "<label id='" + label + "1'>" + label + "</label></td><td>";
  strCode += "<input type='text' id=" + paraId +" value="+parameter.minValue + "-" + parameter.maxValue+ ">";
  $("#" + paraId).attr('value', parameter.minValue + "-" + parameter.maxValue);
  $("#" + paraId).attr('text', parameter.minValue + "-" + parameter.maxValue);
  strCode += "</td></tr>";
  document.body.innerHTML += strCode;
}

Comments

0

You need to initialize the strCode and append it to table. Example

$('#tableId').append(strCode);

Updated Code:

function AddTextBoxes(label, parameter, paraId) {
    var strCode = '';
    strCode += "<tr><td>";
    strCode += "<label id='" + label + "1'>" + label + "</label></td><td>";
    strCode += "<input type='text' id=" + paraId + " value=" + parameter.minValue + "-" + parameter.maxValue + ">";
    $("#" + paraId).attr('value', parameter.minValue + "-" + parameter.maxValue);
    $("#" + paraId).attr('text', parameter.minValue + "-" + parameter.maxValue);
    strCode += "</td></tr>";

    $('#tableId').append(strCode); // Change tableId with your table id
}

Comments

0
var myRow="<tr><td>my row</td></tr>";
$("#myTableId").append(myRow);

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.