1

I have a problem adding an input with similar class(addinput_units).

In the example when you click on the "add" link next to "field 1", it adds a new input following "field 2" with value "field 1".

How can I add a new field following the input nearest to the "add" link with a similar class(addinput_units) in HTML?

EXAMPLE: http://jsfiddle.net/FpsPh/

$(function () {
    $('a.add_input').live('click', function (event) {
        event.preventDefault();
        var $class = '.' + $(this).closest('div.find_input').find('div').attr('class').split(" ")[0];
        var newDiv = $($(this).closest($class).get(0)).clone(false);
        $(this).closest($class).find('.add_input:first').remove()
        newDiv.hide().fadeIn('slow');
        $($class + ':last').after(newDiv);
    });
});

EDITE:

this is the original my code that not work true. see you: http://jsfiddle.net/FpsPh/4/

3 Answers 3

2

I'm not 100% sure I understood the question, but is this what you're going for?

$(function () {
    $('a.add_input').live('click', function (event) {
        event.preventDefault();
        var $this = $(this),
            $div = $this.closest('div'),
            $clone = $div.clone().hide()
                .insertAfter($div).fadeIn('slow');
        $this.remove();
    });
});

Here's the updated jsFiddle.

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

2 Comments

this is the original my code that not work true. see you: jsfiddle.net/FpsPh/4
I've updated your jsfiddle with my JavaScript (jsfiddle.net/FpsPh/5), and it still works the way I'm expecting. If this is not the behavior you want, can you try to explain the desired behavior again? I had trouble understanding your initial question.
1

Your problem is that you are selecting the last addinput_units div in the document, not the one in the correct td. Use closest:

$('a.add_input').live('click', function (event) {
    event.preventDefault();
    var $class = '.' + $(this).closest('div.find_input').find('div').attr('class').split(" ")[0];
    var newDiv = $(this).closest($class).clone(false);
    newDiv.hide().fadeIn('slow');
    $(this).closest($class).append(newDiv).find('.add_input:first').remove()
});

You also don't need to call get(0) on the closest - it will only ever return one element.

Demo: http://jsfiddle.net/FpsPh/2/

1 Comment

this in the original my code not work true. see you: jsfiddle.net/FpsPh/4
0

Try using prev() instead of closest()? Or maybe a different method of finding the element. Maybe matching an ID to it and changing the link's ID, try playing around with different types of ways to find the element you want to clone.

1 Comment

this is the original my code that not work true. see you: jsfiddle.net/FpsPh/4

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.