1

I was wondering if I could get some help with jquery. This is the html I have now:

http://jsfiddle.net/spadez/9sX6X/4/

<form name="input">
<input id="current" type="text" name="content" placeholder="content">
<input type="submit" value="Add" id="add">
</form>

What I want to do is when the user clicks "add" and as long as the input field isnt empty, I want to do the following:

  • Change that input field id to "accepted" and remove the add button next to it and replace it with a remove button
  • Add an input field below the original one with the id to "current" with an add button next to it

I want this process to happen every time the add button is clicked so more and more input boxes can spawn under each other. As far as I got was trying to spawn input boxes but even that doesn't work.

function addField(){
$('#add').append('<input type="text" name="myinput" />');
return false;
}

Can anyone please show me how this should be achieved.

4
  • You are appending to the wrong element, an input can't contain another element Commented Jun 19, 2013 at 11:36
  • If you try to change the id to accepted for every input type , you will face problems.Ids should be unique Commented Jun 19, 2013 at 11:36
  • You need to append to the form, not the submit button for starters Commented Jun 19, 2013 at 11:36
  • You are going to end up with confusing code to maintain if controls are changing id. If you need to mark a control with a certain property you should add a class or property - not change the id Commented Jun 19, 2013 at 11:37

3 Answers 3

3

I think it isn't a good solution to clone existing elements every time. Instead I would suggest cloning ... well... clones )) The original input and button remain the same, only visually shifted.

Is this behavior needed? - http://jsfiddle.net/skip405/9sX6X/6/

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

Comments

1

Use before()

$('#add').before('<input type="text" name="myinput" />');

since you need to append the new input just before your submit button (and not into it, which is not possible here, since it's an input element)

1 Comment

you're not calling the addField function. Right now when you click the button you're just sending the form: try this: jsfiddle.net/MnAGZ/1
0

A working example http://jsfiddle.net/steelywing/9sX6X/12/

$('form').on('click', '.add', function () {
    var row = $(this).closest('div'),
        new_row = row.clone();

    row.find('input:text').addClass('accepted');

    row.find('.add')
        .removeClass('add')
        .addClass('remove')
        .val('Remove');

    new_row.find('input:text').val('');

    row.after(new_row);
}).on('click', '.remove', function () {
    $(this).closest('div').remove();
});

1 Comment

I would suggest removing the ids because they are cloned in your version

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.