Search code examples
jqueryclone

Jquery edit multiple children at once during clone


I am currently using the following code. Which clone an element and changed an input in the clone to what I need it to be. There is a select in the clone as well that I need to change it's name to another val. How do I execute 2 children on the same clone command at the same time?

Code:

$(this).parent().parent()
    .append($(this).parent()
    .clone()
    .children('input').attr('name', VAL)
    .end()
);

I need something like this

$(this).parent().parent()
    .append($(this).parent()
    .clone()
    .children('input').attr('name', VAL)
    .children('select').attr('name', VAL2) // Where this does not target children of the input, but of the clone.
    .end()
);

Solution

  • This answer is with some thanks to gdoron, but he refused to change his answer to the actual correct answer. If you need to edit multiple children in a clone or in any object simple use end and specify the next child.

    $(this).parent().parent()
        .append($(this).parent()
        .clone()
        .children('input').attr('name', VAL)
            .end()
        .children('select').attr('name', VAL2) 
            .end()
    );