Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery append() and remove() element

Tags:

jquery

append

I have a form where I'm dynamically adding the ability to upload files with the append function but I would also like to be able to remove unused fields. Here is the html markup

<span class="inputname">Project Images:     <a href="#" class="add_project_file"><img src="images/add_small.gif" border="0"></a> </span> <span class="project_images">     <input name="upload_project_images[]" type="file" /><br/> </span> 

Right now if they click on the "add" gif a new row is added with this jquery

$('a.add_project_file').click(function() {     $(".project_images").append('<input name="upload_project_images[]" type="file" class="new_project_image" /> <a href="#" class="remove_project_file" border="2"><img src="images/delete.gif"></a><br/>');     return false; }); 

To remove the input box i've tried to add the class "remove_project_file" then add this function.

$('a.remove_project_file').click(function() {     $('.project_images').remove();     return false; }); 

I think there should be a much easier way to do this. Maybe i need to use the $(this) function for the remove. Another possible solution would be to expand the "add project file" to do both adding and removing fields.

Any of you JQuery wizards have any ideas that would be great

like image 725
Brooke. Avatar asked Sep 30 '09 23:09

Brooke.


People also ask

How remove and append in jQuery?

jQuery uses: . append(); and . remove(); functions to accomplish this task. We could use these methods to append string or any other html or XML element and also remove string and other html or XML elements from the document.

Which method are used to add and remove the content in jQuery?

The method used for adding or removing the class to an element is the toggleClass() method.

How do I remove something from jQuery?

To remove elements and content, there are mainly two jQuery methods: remove() - Removes the selected element (and its child elements) empty() - Removes the child elements from the selected element.


2 Answers

Since this is an open-ended question, I will just give you an idea of how I would go about implementing something like this myself.

<span class="inputname">     Project Images:     <a href="#" class="add_project_file">         <img src="images/add_small.gif" border="0" />     </a> </span>  <ul class="project_images">     <li><input name="upload_project_images[]" type="file" /></li> </ul> 

Wrapping the file inputs inside li elements allows to easily remove the parent of our 'remove' links when clicked. The jQuery to do so is close to what you have already:

// Add new input with associated 'remove' link when 'add' button is clicked. $('.add_project_file').click(function(e) {     e.preventDefault();      $(".project_images").append(         '<li>'       + '<input name="upload_project_images[]" type="file" class="new_project_image" /> '       + '<a href="#" class="remove_project_file" border="2"><img src="images/delete.gif" /></a>'       + '</li>'); });  // Remove parent of 'remove' link when link is clicked. $('.project_images').on('click', '.remove_project_file', function(e) {     e.preventDefault();      $(this).parent().remove(); }); 
like image 171
Alex Barrett Avatar answered Sep 17 '22 09:09

Alex Barrett


You can call a reset function before appending. Something like this:

    function resetNewReviewBoardForm() {     $("#Description").val('');     $("#PersonName").text('');     $("#members").empty(); //this one what worked in my case     $("#EmailNotification").val('False'); } 
like image 38
boateng Avatar answered Sep 21 '22 09:09

boateng