0

I have an "add" button that inserts this row everytime it is clicked. Each row has an Edit and Delete button.

//html
<tr class="insertedRow">
    <td><input type="hidden" class="name" /></td>
    <td><input type="hidden" class="age" /></td>
    <td>
        <input type="button" class="edit" class="btn" value="Edit" />
        <input type="button" class="delete" value="Delete" />
    </td>
</tr>

I have this jQuery to Delete the corresponding row that you want deleted. But I can't figure out how to select "name" and "age" of the corresponding row when you click "Edit" The code I have right now is always targeting the first inserted row. Any ideas? Thanks.

I think I need to somehow select the parent.parent of the "Edit" button and then target the "name" and "age" id's.

//jQuery
$(".delete").live('click',function() {
    $(this).parent().parent().remove();
});

$(".edit").live('click',function() {
    $('.name').clone().attr('type','text').insertAfter('.name').prev().remove();
    $('.age').clone().attr('type','text').insertAfter('.age').prev().remove();
});
user1738750
  • 205
  • 4
  • 15

5 Answers5

0

first convert id to class:

<td><input type="hidden" class="name" /></td>
<td><input type="hidden" class="age" /></td>

then :

$("#edit").live('click',function() { $name =
   var name =  $(this).closest(".insertedRow").find(".name");
   var age =  $(this).closest(".insertedRow").find(".age");

   name.clone().attr('type','text').insertAfter(name).prev().remove();
   age.clone().attr('type','text').insertAfter(age).prev().remove();
});
mgraph
  • 15,238
  • 4
  • 41
  • 75
0

You can't clone an element with an id attribute on it, unless you change its id.

Please read this to understand why it gives you the first element every time.

Community
  • 1
  • 1
gdoron
  • 147,333
  • 58
  • 291
  • 367
0

to avoid duplicated id, you can do something like this

var lastid = $('[id^="clonedInput"]').length + 1;
$(".clonedInput").last().clone().attr('id', 'clonedInput' + lastid).appendTo("body")

or use a .class

RASG
  • 5,988
  • 4
  • 26
  • 47
0

This are the values:

$(this).parent().find(".name").text();
$(this).parent().find(".age").text();

and your edit function:

$(".edit").live('click',function() {
    $('.name').clone().attr('type','text').insertAfter('.name').value($(this).parent().find(".name").text()).prev().remove();
    $('.age').clone().attr('type','text').insertAfter('.age').value($(this).parent().find(".age").text()).prev().remove();
});
Udan
  • 5,429
  • 2
  • 28
  • 34
0

For one thing, your click event handler is targeting an ID named '#name'... but your cloning of the rows ensures you're going to have multiple elements with the same ID.

Also, jQuery's documents make it fairly clear that changing the type attr is prohibited: http://api.jquery.com/attr/

Note: jQuery prohibits changing the type attribute on an or element and will throw an error in all browsers. This is because the type attribute cannot be changed in Internet Explorer.

Also, as others have pointed out, you probably should not be using .live() as it's deprecated (means it's going away in the indeterminate future)... you should look at using .on(), as that's the supported direction.

Jason M. Batchelor
  • 2,951
  • 16
  • 25
  • Okay thanks for the awnser. I thought clone was a work around for changing the type attr. – user1738750 Oct 18 '12 at 16:34
  • If you're going to those lengths, rather than touching the DOM so many times in one call, it might be a lot better for you to simply create a string representation of what you want to make, and insert that with .append(). The more often you do a selector-call, the more expensive the call is, as it forces jQuery to reparse the DOM and find elements for you. Last I checked, `.html("
    mow mow
    ")` is still more performant than `$("
    ").attr('blah','boo').html('mow mow').css('border','red')`.
    – Jason M. Batchelor Oct 18 '12 at 16:45