javascript - Dynamically create textbox on click button, and change it's css properties for selected textbox - Stack Ove

Thanks once again,for all great mind's.My expectation is exactly what the they have in example sit

Thanks once again,for all great mind's.

My expectation is exactly what the they have in example site.

example site:

--->creating text box dynamically onclick in CANVAS area only.

--->text box input text's font,font size,color .. changed by getting id var somename = document.getelementbyid("id of textbox -in this case id getting uniquely"); somename = here all function for changing color, font are follows

but,

my problem is to add text box dynamically onclick button,so how to assing id for var somename = document.getelementbyid("id of textbox(dynamically created text box id)");

At the same time that text box should be movable any were in canvas.

Thanks once again,for all great mind's.

My expectation is exactly what the they have in example site.

example site: http://www.printvenue./customer-design/editor/rounded-corner-business-cards/3-0128-vc-psd

--->creating text box dynamically onclick in CANVAS area only.

--->text box input text's font,font size,color .. changed by getting id var somename = document.getelementbyid("id of textbox -in this case id getting uniquely"); somename = here all function for changing color, font are follows

but,

my problem is to add text box dynamically onclick button,so how to assing id for var somename = document.getelementbyid("id of textbox(dynamically created text box id)");

At the same time that text box should be movable any were in canvas.

Share Improve this question edited Feb 19, 2014 at 11:17 user3304886 asked Feb 18, 2014 at 18:25 user3304886user3304886 11 gold badge1 silver badge2 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 1

You can create a new HTML element like this:

var textBox = document.createElement("textarea");

To add it to the parent use

document.getElementById([insert id of parent here]).appendChild(textBox);

This parent should be the div/html-element in which all the textareas are located.

Example in JSFiddle

HTML

<div id='parent'>
    <textarea>txt1</textarea>
    <textarea>txt2</textarea>
    <textarea>txt3</textarea>
</div>
<button onClick="addBox()">add textarea</button>

JS

addBox = function(){
    var textBox = document.createElement("textarea");
    document.getElementById("parent").appendChild(textBox);
}

Example in JSFiddle with JQuery

HTML

<div id='parent'>
    <textarea>txt1</textarea>
    <textarea>txt2</textarea>
    <textarea>txt3</textarea>
</div>
<button>add textarea</button>

jQuery

$(function(){
    $('button').on('click', function(){
        var textBox = document.createElement("textarea");
        $('#parent').append(textBox);
    });
});

Here the link that you wanted :

JavaScript Version:

HTML

<input id='inp' type='button' value='Click me'/>
<div id='cont'>

</div>

JavaScript

document.getElementById('inp').addEventListener('click', function () {
        var textarea = document.createElement('textarea');
        textarea.className="mytextbox";
        document.getElementById('cont').appendChild(textarea);
    });

Css

.mytextbox{
    width:200px;height:200px;box-shadow:2px 1px 5px 1px #000;

}

jQuery Version :

HTML

<input id='inp' type='button' value='Click me'/>
<div id='cont'>

</div>

jQuery

$('input').click(function(){

     var textarea =  $('<textarea></textarea>');
     textarea.css({'width':'200px','height':'200px','box-shadow':'1px 2px 5px 1px #000'});
    $('#cont').append(textarea);

});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745172697a4615023.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信