0

Sorry for the vague title, couldn't think of how to title my question. So I've made a html/css table wherein I placed an edit button for each row. Pressing the button would transform a certain text field into a text-input field there by allowing the user to edit the data inside; at the same time the edit button then transforms to save button. After editing , the user then would press the save button and the text-input field would then revert back into a text field and the save button back to an edit button. But it's not entirely working

This is my problem, after editing the data inside, upon pressing the save button, the data inside is deleted and replaced with: <input type= and outside the text-input field is: " /> and the save button remains as is, a "SAVE" button and not back to an "EDIT" button

here's the script:

$(document).ready(function () {

    // listen for email edit button click
    $("button#edit").on('click', function () {
        // get email inline to this edit button
        var email = $(this).parent().siblings('td.email').html();
        alert(email);

        // change button from edit to save
        $(this).attr('id', 'save-email').html('SAVE');

        // change email display to input field
        $(this).parent().siblings('td.email').html('<input type="text" id="user-email" value="' + email + '" />');
    });

    // listen for email save button click
    $("button#save-email").on('click', function () {

        // get new email inline to this save button
        var newEmail = $(this).parents('tr').find($('input#user-email')).val();
        alert(newEmail);

        // change input field back to display
        $(this).parent().siblings('td.email').html(email);

        // change button from save to edit
        $(this).attr('id', 'edit').html('EDIT');
    });
});  

sorry for the wall of text.

1
  • // change input field back to display $(this).parent().siblings('td.email').html(email); here you wont be getting the value of "email" Commented Jul 31, 2013 at 11:31

4 Answers 4

1

try to put it this way :

$(document).on('click',"button#save-email", function() {...

instead of

$("button#save-email").on('click', function()

using the second one won't work, because when it is run, $("button#save-email"), doesn't exist yet.

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

Comments

0

The problem is that the first handler is the only one attached in script.

Your javascript code is executed when the page is ready.
At that moment only the first handler is attached because there is not yet an element button#save-email. When you click save, it is the first handler that is executed again, not the one think !

Why don't you create two distinct buttons and show one or the other ?

<table>
    <tr>
        <td class="email">[email protected]</td>
        <td>
            <button class="btn-edit">Edit</button>
            <button class="btn-save" style="display:none">Save</button>
        </td>
    </tr>
    <tr>
        <td class="email">[email protected]</td>
        <td>
            <button class="btn-edit">Edit</button>
            <button class="btn-save" style="display:none">Save</button>
        </td>
    </tr>
</table>

Javascript

// listen for email edit button click
$('.btn-edit').on('click', function() {

    var $this = $(this),
        $parentRow = $this.closest('tr');

    // get email inline to this edit button
    var email = $this.parent().siblings('td.email').html();

    // change button from edit to save
    $parentRow.find('.btn-edit').hide();
    $parentRow.find('.btn-save').show();

    // change email display to input field
    $(this)
        .parent()
        .siblings('td.email')
        .html('<input type="text" id="user-email" value="'+email+'" />');
});

// listen for email save button click
$('.btn-save').on('click', function() {

    var $this = $(this),
        $parentRow = $this.closest('tr');

    // get new email inline to this save button
    var newEmail = $(this).parent().siblings('td.email').find('#user-email').val();

    // change input field back to display
    $(this).parent().siblings('td.email').html(newEmail);

    // change button from save to edit
    $parentRow.find('.btn-edit').show();
    $parentRow.find('.btn-save').hide();

});

DEMO

4 Comments

oh, okay, I assumed that what I did would automatically create a new handle but I guess it wouldn't. I integrated what you did and it finally worked for me. Thank you!
Hello Didier, follow-up question, there are multiple rows in my table but I don't understand why the other edit buttons are not working. Only the edit button in the first row. Any ideas why it's acting that way?*I'm thinking I should have mentioned that in my scenario above.
My example was working only with one row (i had specified it in comments - for the sake of the example). I have changed the code a bit to work with classes and in the scope of a row.
I'm learning a lot. Thank you so much Didier! Concepts I've learned will certainly be put to good use in the future.
0

Take a look at KnockoutJS, which utilizes nice Bindings helping you to do things like that much easier. Here is an example of an editable grid with KnockoutJS

KnockoutJS Editable Data example

2 Comments

thanks for the hint though I don't intend for the field to be always editable since the table is intended for an on-site registration for an event.
Just because I saw ... shouldn't the last line of your click handler be .val() instead of .html()?
0

Neat & Works

<table>    
  <tr>
    <td>some text</td>
    <td><input type='button' value='edit' id='btn'/></td>
  </tr>        
</table>


$(document).ready(function() {
$("#btn").click(function() {
    var $btn=$(this);

    var opn= $btn.val()


    switch(opn){

        case 'edit':
            var $td=$(this).parent().parent().find('td:first');
            var email=$td.html();
            $td.html("").append('<input type="text" value="'+email+'">');
            $btn.val("save");
            break;

        case 'save':
            var $input=$(this).parent().parent().find('td:first input');
            var email=$input.val();
            $input.parent().html(email);
            $btn.val("edit");
            break;    
   }
 });
});

http://jsfiddle.net/h55rc/

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.