Mandirigma
Mandirigma

Reputation: 29

JQuery, Javascript button

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.

Upvotes: 0

Views: 169

Answers (4)

Didier Ghys
Didier Ghys

Reputation: 30666

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

Upvotes: 0

user2587132
user2587132

Reputation:

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/

Upvotes: 0

zewa666
zewa666

Reputation: 2603

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

Upvotes: 0

Dany Y
Dany Y

Reputation: 7031

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.

Upvotes: 1

Related Questions