5

This is my bootstrap table.I want to insert data into table using jQuery only.On clicking a particular cell,a textbox should be open to enter data.I don't want to use any other plugin.

       <table class="table table-bordered">
         <thead class="mbhead">
           <tr class="mbrow">
             <th>A</th>
             <th>B</th>
             <th>C</th>
             <th>D</th>
           </tr>
        </thead>
        <tbody>
          <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
          </tr>
          <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
          </tr>
          <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
          </tr>
          <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
          </tr>
          <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
          </tr>
          <tr>
            <td></td>
             <td></td>
            <td></td>
            <td></td>
          </tr>
          <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
          </tr>
           <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
          </tr>          
         </tbody>
       </table>

help me.thanx.

3
  • You must be having any database for this? Commented Mar 1, 2013 at 5:48
  • Right now,i am not thinking about database..just make it editable only Commented Mar 1, 2013 at 5:49
  • 3
    this might be helpful jsfiddle.net/JPVUk/1 Commented Mar 1, 2013 at 6:03

1 Answer 1

8

You basically want to add an event to the user clicking on a table row. In jQuery you can add that event like this

$("table.table tr td").bind("click", dataClick);

In the dataClick function you wan to make the row editable. You can do so like this.

function dataClick(e) {
    console.log(e);
    if (e.currentTarget.innerHTML != "") return;
    if(e.currentTarget.contentEditable != null){
        $(e.currentTarget).attr("contentEditable",true);
    }
    else{
        $(e.currentTarget).append("<input type='text'>");
    }    
}

I have a sample here, http://jsfiddle.net/JPVUk/4/

Hope this helps

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

2 Comments

@Amrendra Try using live() instead of bind(). Like this, jsfiddle.net/JPVUk/5
live() was deprecated in jQuery 1.7 and removed in 1.9. You can put it back with the jQuery migrate plugin but I'm not so sure that's a good idea going forward.

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.