2

So im creating project in which you can create unlimited number of input fields that belong to the same array and eventualy are being posted to php handler via ajax. i managed to get that far, its working all fine but the problem im having is that i would want to let user to delete input he/she doesnt want (i.e. created by mistake), it seems to be core part of script, yet i dont know how to approach the issue.

This project you can see in here:

http://jsfiddle.net/7LCzN/

and this is the code:

$(function(){
    $("#add").on('click', function () {
        $('body').append('<input type="text" class="ac" name="array[]" />');
    });
});

$(function(){
    $("#post").on('click', function () {

        var myArray = new Array();

        $('.ac').each(function(index, elem) {
            myArray.push($(elem).val());
        });
        $('#result').text(myArray);
    });
});

So for instance ive created 4 fields with these value:

5463, 8675, 2340, 1203

and i just realized i dont want the one with value 2340 i would want to delete it so im left with 3 fields:

5463, 8675, 1203

anyone that helps, ill be glad and greatful, thank you fellows:)

1
  • You will likely have to insert a UI element to allow the user to trigger a deletion of an input. In your example you have none. What would the user need to do to delete an input in your app? Commented Apr 7, 2014 at 21:31

2 Answers 2

4

.remove() is a jQuery function that you can use to remove elements from the DOM.

Here's a tiny example:

$(".inputToRemove").remove();

Here's a fork of your jsFiddle for a working example.

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

6 Comments

function remove() is known to me but how to use it in that particular case to remove one specific field - that i dont know
Attach a click event listener to your inputs. Inside, simply do $(this).remove();.
And remove from array - var array_num = $( "input" ).index( $(this).prev() ); myArray.slice(array_num, 1); -- jsfiddle.net/d37M3/1
@user3388636 Nope, myArray is local and has no effects beyond its local scope.
this is brilliant mate, this is what i was looking for i will accept it ofc in a moment, last peace of bussiness, could you shortly explain why we use .prev().remove(); and than .remove();, i would want to know, we must learn to improve :)
|
0

From the docs:

Event handlers are bound only to the currently selected elements; they must exist on the page at the time your code makes the call to .on(). To ensure the elements are present and can be selected, perform event binding inside a document ready handler for elements that are in the HTML markup on the page. If new HTML is being injected into the page, select the elements and attach event handlers after the new HTML is placed into the page. Or, use delegated events to attach an event handler, as described next.

Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time. By picking an element that is guaranteed to be present at the time the delegated event handler is attached, you can use delegated events to avoid the need to frequently attach and remove event handlers. This element could be the container element of a view in a Model-View-Controller design, for example, or document if the event handler wants to monitor all bubbling events in the document. The document element is available in the head of the document before loading any other HTML, so it is safe to attach events there without waiting for the document to be ready.

In addition to their ability to handle events on descendant elements not yet created, another advantage of delegated events is their potential for much lower overhead when many elements must be monitored. On a data table with 1,000 rows in its tbody, this example attaches a handler to 1,000 elements:

$( "#dataTable tbody tr" ).on( "click", function() {
alert( $( this ).text() );
});

A delegated-events approach attaches an event handler to only one element, the tbody, and the event only needs to bubble up one level (from the clicked tr to tbody):

$( "#dataTable tbody" ).on( "click", "tr", function() {
alert( $( this ).text() );
});

Comments

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.