Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I need to dynamically add textboxes and for each textbox add delete button besides it which should delete that textbox and then on save button all the data should be available to the controller as i'm using MVC3 Razor view. So I dont have any aspx.cs file i'm using razor view where .cshtml files are used.

I've tried using Jquery but I could not delete the textboxes randomly,
I can delete the last added textbox but not able to figure out how to delete a textbox based on a button next to that textbox

Also i need to save all the data into database.
Can anyone please help!!

Below is the code which i'm using to add textbox i need to add a delete button as well and also that delete button should delete the textbox and buttton itself.


PHP
$("#addButton").live('click', function () {

            if (counter > 21) {
                alert("Only 21 textboxes allow");
                return false;
            }

            var $ctrl1 = $('<p class="list">');
            var $ctrl2 = $('<input/>').attr({ type: 'text', name: 'mediaVehicle' + counter, id: 'mediaVehicle' + counter });
            var $ctrl3 = $('</p>');
            $("#MediaVehiclesGroup").append($ctrl1);
            $("#MediaVehiclesGroup").append($ctrl2);
            $("#MediaVehiclesGroup").append($ctrl3);

            counter++;
        });
Posted

1 solution

Add a button the same way you have added the textbox. What difficulty are you having?

$('.deleteButton').click(function()
{
   var parent = $(this).parent();
   parent.empty();
});


This adds a click event handler to the delete button which will get the parent element, the p tag in this case, and empty all elements in it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900