1

I want to design a simple text area using javascript. I am using the following function

    function add2(type) {
     var element = document.createElement("input");
     var label=prompt("Enter the name for lable","label");
     document.getElementById('raj').innerHTML=document.getElementById('raj').innerHTML+label;
     element.setAttribute("type", type);

     element.setAttribute("name", type);
     element.setAttribute("cols",20);
     element.setAttribute("rows",50);

     var rohit = document.getElementById("raj");
    rohit.appendChild(element);
    document.getElementById('raj').innerHTML=document.getElementById('raj').innerHTML+"<br/>";

}

I am using this function in my HTML code as follow :

<input type="button" value="Text Area" onclick="add2('textarea')">

But when I am executing this code, it is creating only a simple text box. what should I do ??

Thanks

1
  • createElement("input") crearted an input. How do you expect it to be a textarea? Commented Jul 11, 2012 at 12:46

2 Answers 2

2

Textarea's have their own HTML tag, I think that this is what you want:

Demo: http://jsfiddle.net/SO_AMK/ZzdWR/

HTML:

<input type="button" value="Text Area" onclick="add2('textarea')">
<div id="raj">Lorem ipsum...</div>

​JavaScript:

function add2(name) {
     var element = document.createElement("textarea");
     var label = prompt("Enter the name for lable","label");
     var rohit = document.getElementById("raj");
     rohit.innerHTML = rohit.innerHTML + label;

     element.setAttribute("name", name);
     element.setAttribute("cols",20);
     element.setAttribute("rows",50);

    rohit.appendChild(element);
    rohit.innerHTML = rohit.innerHTML + "<br/>";

}​
Sign up to request clarification or add additional context in comments.

Comments

1

Text areas are separate elements:

<textarea>content</textarea>

rather than

<input type="textarea" value="content" />

1 Comment

thanks to all . I have put only createElement("textarea") at place of createElement("input");

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.