Click to See Complete Forum and Search --> : replicating fields


sdev
07-22-2003, 08:38 AM
Hi,
I need some help with creating a survey form that has a question area(textbox) and an answer area(textarea) and a button with a "+" symbol. when a user clicks on the "+" button the question and answer areas need to be replicated (i.e., if the first q/a pair exists another q/a areas should be added)and so on...
Any suggestions/help in regard to this problem is appreciated.

Thanks...

Khalid Ali
07-22-2003, 09:27 AM
This is how you can get this done.
in the onclick event of the button call a function say

onclick="Replicate();"

now in the JavaScript section of the code define this function something like this
function Replicate(){
//get reference to the form that has the fields
var frm = document.getElementById("formId");
//now create new fields to be appended
var newtextField = document.createElement("input");
//now set this type to text
newtextField.setAttribute("type","text");
//you can add many attributes just like above such as id,name,value...etc

//now just append the above new form field to the existing form

frm.appendChild(newtextField);
//there you go...
}

sdev
07-22-2003, 10:29 AM
Thanks Khalid.....
the piece of code u gave me is really helpful.

Khalid Ali
07-22-2003, 10:34 AM
:D
You are welcome

sdev
07-22-2003, 02:28 PM
i had another question to ask :
with the code that is replicating the q/a areas , the text boxes are placed next to each other , though i'm trying to give the <br> tag with document.write("<br>"), so that they come one after another , the above tag doesn't seem to work.

Any suggestions...


Thanx

Khalid Ali
07-22-2003, 02:41 PM
What you'd need to do is create another element just the way I showed you above only this time it will be

document.createElement("br");
and append it to the form element after the text field the order will be this

form.appendChild("textField");
form.appendChild("br_element");

sdev
07-22-2003, 03:01 PM
Awesome.....thanks

Khalid Ali
07-22-2003, 03:02 PM
:D My Pleasure SDev :D