I am dynamically adding input fields along with "x" that allows user to delete this field. Looks like this
Javascript looks like this:
$(document).ready(function() {
// When the add_button is pressed
$('#add_correct_answer').click(function() {
// Container (already created)
var container = $('.buttons-fields-correct');
// Create the input wrapper (input tag + delete button), and append it to `container`
var input_wrapper = $('<div>', { class: 'input-wrapper' }).appendTo(container);
// Create an input tag, and append it to input_wrapper
var input = $('<input>', { name: 'task[correct_answers][]', type: 'text' }).appendTo(input_wrapper);
// Create the remove button, append it to input_wrapper, and add a click callback to
// remove the input wrapper if it's pressed.
var remove = $('<span>', { text: 'x' }).appendTo(input_wrapper).click(function() {
input_wrapper.remove();
});
});
Related HTML:
<%= label :form_type, t(:add_correct_answers) %>
<div class="buttons-wrapper">
<%= button_tag t(:add_field), id: 'add_correct_answer', type: 'button' %>
<div class="buttons-fields-correct">
<div>
</div>
</div>
</div>
What I want to do is to give this "x" a class that I could modify and change appearance/position of this "x". For example position it inside input on right side.
.addClass("classHere")beforeappendto()addclass is a jQuery function and can be used as part of the "chain" also read about.toggleClass()and.removeClass()class: '<yourclass>'inside the argument list.$('<div>', { class: 'input-wrapper' })