If we click the “Add” link, a new input should be added to the form.
Also, there should be link “Delete” near each added input. If we click on it, this input should be removed.
How do I do this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I’m struggling whether to vote to close this as a duplicate, even though it’s not exact. But what you’re looking for appears to be answered here. The main difference is that where they are binding to the
changeevent for aselectelement:you would instead bind to the
clickevent of an anchor element (your “Add” link):The “Delete” link would work in a similar manner. As the HTML is created for the new input, you’d also create the “Delete” link to go next to it before adding it to the DOM (the call to the
.html()function in the referenced code). You’ll want to make sure that you give the added HTML a uniqueid(wrapped in adivwith a uniqueidfor example), as well as to the added “Delete” link.Then, after it’s added to the DOM, you’d add an event handler to the “Delete” link:
In this handler you’d reference the unique
idof the container of what was added to the DOM and remove it. I’m sure there’s a more elegant way to handle this by using the.live()function here and some more clever selectors. It would be interesting to re-factor something like this while trying to not be too clever so as to hurt ongoing support.There are some additional considerations you’ll want to keep in mind as you develop this:
hrefattribute from them entirely, but then you’ll also need to style them to visually behave like links. Not difficult, but not intuitive either. Why not buttons?