0

Does anyone know an simple/easy way or the best way to add fields dynamically in a form, using JQuery in rails3?

1

1 Answer 1

4

Regardless what server-side language you're using, you can just create elements with the jQuery constructor and append it to a form node. For instance:

$('<input>', {
    id:   'my_new_input_id',
}).appendTo($('#my_form_id'));

would create a new input control and append it to a form with the id my_form_id.

edit

relating to your comment: To remove a dynamically created element, a good approach is to store a reference in a variable. Doing that you can call .remove() or .detach() later:

var my_new_input_element = $('<input>', {
    id:   'my_new_input_id',
}).appendTo($('#my_form_id'));

// ... lots of code

my_new_input_element.remove();

ref.: .appendTo, .remove(), .detach()

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

1 Comment

Thanks jAndy, that worked for adding - any idea how to remove fields?

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.