0

I'm trying to create a script where I can:

  1. Add data and append it on the table; and
  2. Edit the data that has been entered.

So far, this is my code:

HTML:

<table class="tbl" style="width: 48%;">
    <tr>
        <td align="center">
            Name</td>
        <td align="center">
            Gender</td>
        <td align="center">
            Contact Number</td>
        <td align="center" width="70px">
            </td>
    </tr>
    <tr>
        <td>
            <input id="txtName" type="text" /></td>
        <td>
            <select id="cmbGender" name="gender">
                <option value="Male">Male</option>
                <option value="Female">Female</option>
            </select></td>
        <td>
            <input id="txtContact" type="text" /></td>
        <td align="middle">
            </td>
    </tr>
    </table>
<button id="btn1">Add</button>

then here's my Javascript:

 $(document).ready(function() {
var num = 0;

    $("#btn1").click(function() {
            num += 1;
        $(".tbl").append("<tr><td>" + $("#txtName").val() + "</td><td>" + $("#cmbGender").val() + "</td><td>" + $("#txtContact").val() +"</td><td><button id=\"edit"+num+"\">Edit</button></td></tr>");
});

for(var i = 1; i <= num; i++) {
    $("#edit"+i).click(function(){
        alert(".tbl tr:eq("+i+")");
        $(".tbl tr:eq("+i+")").append("<tr><td><input type=\"text\" value=\""+$("tr:eq("+i+") td:eq(0)").val()+"\" /></td><td><select><option>Male</option >Male<option></option></select></td><td><input type=\"text\" value=\""+$("tr:eq("+i+") td:eq(2)")+"\" /></td><td></td></tr>");
    });
}
});

Edit : one more thing. It'll be in a form to be processed and sent to the database.

3
  • What is your specific question? Where are you stuck? Commented Jan 28, 2013 at 4:30
  • if you need maximum flexibility, do it yourself with css display tables; otherwise, use a plug like in answer below Commented Jan 28, 2013 at 4:46
  • @ExplosionPills I need a way where once you clicked the edit button on a particular row, that row will be in sort of 'edit mode' (the text changes into textbox, etc.). I'm trying to know how to identify which row the edit button was clicked. Commented Jan 28, 2013 at 5:08

1 Answer 1

1

I'd suggest using one of the several existing javascript libraries that already do this very well. My favorite is the DataTables jQuery plugin paired with the DataTables Data Manager Plugin or the jEditable plugin

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

2 Comments

I've looked-up on the links that you've provided and I forgot that my code will be inside an html form and thus it'll be inputted by the user. The links you've provided are more of a datagrid view and needs a data source, isn't it?
I'm not sure exactly what you mean, but DataTables is extremely flexible - the usage can be as simple as wrapping an existing html table, or you can have a 'client side' data source (e.g. javascript literals), or you can have a server-side data source via ajax.

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.