2

My button and table is as shown,

<button type="button" class="btn btn-md" id="addRowBtn">Test Button To Add Row</button>
<table id="tableForRows">
     <tr>
        <td>

        </td>
    </tr>
</table>

I want to keep adding text boxes on click of button..My jquery is as shown,

$(document).ready(function () {
    $("#addRowBtn").on("click", function () {
        var tableRow = $("#tableForRows").html();
        var itemToAdd = "<tr><td><input type="text"/></td></tr>";
       $("#tableForRows").append(itemToAdd);
    });
});

Help me know what the right way is..Thanks in advance..

2
  • Any error in console? Commented May 8, 2015 at 15:02
  • This is a typo with your quotes. The syntax highlighting in your question shows that. Commented May 8, 2015 at 15:15

2 Answers 2

4

You have quotation problem, use single quotes for type='text':

"<tr><td><input type='text'/></td></tr>";

Also, based on what you shared, var tableRow = $("#tableForRows").html(); is redundant.

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

1 Comment

To add to this, you can also use "<tr><td><input type=\"text\"/></td></tr>";. Using backslashes will escape the quote character.
1
$(document).ready(function () {
    $("#addRowBtn").click(function () {
        var tr = document.createElement("tr");
        var td = document.createElement("td");
        var input = document.createElement("input");
        input.type = "text";
        $("#tableForRows").append(tr);
        $("#tableForRows tr:last-child").append(td);
        $("#tableForRows tr:last-child td:last-child").append(input);
    });
});

JSfiddle: https://jsfiddle.net/Neoares/Lo6p02oc/

1 Comment

You should add the input to the td, then the td to the tr, then the tr to the table. It will save you the last three selectors, which can be quite slow. Something like: jsfiddle.net/Lo6p02oc/2

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.