The site I am working on has a few forms that allow the user to add new form fields(textbox,textarea ect) dynamically. I am using a structure smiler to:
<label>Name : </label> <input type = "text" / >
<label>Age : </label> <input type = "text" />
<span id = "add_field"> Add Field </span>
//create a new field then append it before the add field button
var new_field = " <label>Name : </label> <input type = "text" / >";
new_field+="<label>Age : </label> <input type = "text" />";
My questions are : Which is a better way of doing this type of thing? And why is one better than the other? If it is better to create the field client-side should I change how I am doing it? Instead of creating a string representing html and appending it should I be doing it a different way?Any tips or advice on how to do this better would be very much appreciated, thanks much!
However, Ajax has nothing to do with this... Those sites use Ajax because they have to fetch data based on the user action, and they have estimated that keeping all data for all possible user actions is not efficient for them.
Your demo and the code seems to be working perfectly fine but what if you want to process the form with PHP and send the result to an email? How to dynamically add unique 'name' and 'id' attributes to the form <input> element? Let's say the name and id for the first input box is name="name1" and id="id1" .. how to dynamically generate 'name2' and 'id2' for the second input box while creating a new field?
Hi, by unique do you mean a number or a word? There are a few ways you could do it. After you clone your item make changes to the id/name properties as well before appending it. Could you give an example of how your layout is and what types of fields you need to be unique?
Leave a comment on Jstall's reply
Change topic type
Link this topic
Provide the permalink of a topic that is related to this topic