0

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 -->

<?php echo $javascript->link('prototype');
echo $javascript->link('scriptaculous'); ?>


<!--script starts here -->

<script type="text/javascript">
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)";
    }

</script>

</div>

<table border=0><tr>

<td width="10%">

 <button style="width:10" onclick="javascript: setformname()">Set the FormName</button><br><br><br>
<button style="width:10" onclick="createtext();">TextBox</button><br><br><br>

<button style="width:10" onclick="createtextarea();">TxtArea</button><br><br><br>

</td>
<td width="75%">

<div id="hold">

<label id="myForm">My  Form </label>
</div></td>
</table>
3
  • Proper formatting is important! Commented Mar 19, 2009 at 11:40
  • 1
    @Aruna, you should reduce your problem to a smaller, reproducible piece of code. That way you'll have greater chances to obtain a response or even more, you may find the answer for yourself. Commented Mar 19, 2009 at 11:46
  • I don't get the var e1=$('hold'); e.appendChild(myForm); Commented Mar 19, 2009 at 11:55

3 Answers 3

1

Community wiki for iteratively improving the code:

function createtext()
{
    var txt1=document.createElement('input'); // Create an input element [textbox]
    var 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()
{
    var txt2=document.createElement('textarea'); // Create an input element [textbox]
    var 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)";
}
Sign up to request clarification or add additional context in comments.

Comments

0

A cursory examination of your code didn't reveal any obvious errors, though you seem to be using a lot of variables with global scope so it's always possible that you've got some conflicts that we can't see. One potential problem may be that you're reusing ids since you rely on the same variable to generate the ids for both. You could test this by having your textboxes named textNNN instead of fieldNNN.

Other than that, my recommendations would be to use Firebug in Firefox and set some breakpoints in the code to see what values are being assigned. Beyond that, you might want to think about how you could simplify your code by refactoring to smaller, reusable functions (say for creating a label, creating a field) using parameters to provide the attribute values. Since it appears you are using some sort of framework -- the $('hold') is idiomatic of several -- you might also want to see if your framework has a way to handle creating DOM elements in a more elegant manner.

1 Comment

ya framework is working correct..The problem is with the Javascript..If i clicked Textarea ,the Textarea is displayed only if any Textbox is created
0

Some element does not have the method appendChild() like labels and buttons. And when we use these elements, than IE generates the error message

Unexpected call to method or property access

In other browsers, they are smarter than IE. They render the content. That's why the appendChild() works fine for the labels, buttons etc. for the IE.

The simple solution is to use objects that support the appendChild() method like div, body and head instead of label etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.