I have two functions inside the javascript: createtext
and createtextarea
.
If I first click on the button to createtext()
, the textbox is shown and the other elements are also created correctly.
However, if I click on the button for createtextarea()
the textarea is not shown until clicking to createtext()
.
I think textarea is only displayed after it gets a textbox there to append...
<!-- File: /app/views/posts/index.ctp -->
link('prototype'); echo $javascript->link('scriptaculous'); ?>
var myForm;var fieldidctr=0;var labelidctr=0; window.onload=function() { // Create a form myForm = document.createElement('FORM'); myForm.method = 'post'; myForm.id = 'myForm'; fieldidctr=1; labelidctr=1; } function createtext() { txt1=document.createElement('input'); // Create an input element [textbox] label1=document.createElement('label');//create a label var tnode=document.createTextNode("click to edit label "); label1.appendChild(tnode); label1.onclick=function(){createLabel(tnode);} txt1.setAttribute('type','text'); // Set the element as textbox var txtid="field"+fieldidctr; fieldidctr++; var link = document.createElement("A"); //link.href = "http://localhost/cake_1.2.1.8004/index.php/posts"; var atext = document.createTextNode("Remove"); link.appendChild(atext); link.onclick=function(){$(txtid).parentNode.removeChild($(txtid)); $(labelid).parentNode.removeChild($(labelid)); myForm.removeChild(link); } var labelid="labelid"+labelidctr; labelidctr++; txt1.setAttribute('id',txtid); label1.setAttribute('id',labelid); txt1.onchange=function(){var userInput = txt1.value; txt1.setAttribute('value',userInput);} myForm.appendChild(label1); myForm.appendChild(txt1);//add the textbox to the MyForm myForm.appendChild(link); var e=$('hold'); e.appendChild(myForm); myForm.onSubmit="save_field(this.txt1)"; } function createLabel(myForm) { myForm.data=prompt("Enter the label name "); } function createtextarea() { txt2=document.createElement('textarea'); // Create an input element [textbox] label2=document.createElement('label');//create a label var tnode=document.createTextNode("click to edit label "); label2.appendChild(tnode); label2.onclick=function(){createLabel(tnode);} txt2.rows='10'; txt2.cols='3'; var txtid="field"+fieldidctr; fieldidctr++; var labelid="labelid"+labelidctr; labelidctr++; var link = document.createElement("A"); var atext = document.createTextNode("Remove"); link.appendChild(atext); link.onclick=function(){$(txtid).parentNode.removeChild($(txtid)); $(labelid).parentNode.removeChild($(labelid)); myForm.removeChild(link); } txt2.setAttribute('id',txtid); label2.setAttribute('id',labelid); txt2.onchange=function(){var userInput = txt2.value; var txtnode=document.createTextNode(userInput); txt2.appendChild(txtnode); } myForm.appendChild(label2); myForm.appendChild(txt2);//add the textarea to the MyForm myForm.appendChild(link); var e1=$('hold'); e.appendChild(myForm); myForm.onSubmit="save_field(this.txt2)"; } Set the FormNameTextBox
TxtArea
My Form