View Full Version : Creating text element using Javascript
Hi,
Can anyone pls tell me how to create a text element dynamically when ever i click on a button using JAvascript..
Thanks in Advance
Avis.
beetle
10-11-2002, 03:28 PM
Well, you need something to append it to, but here's a start...
function makeText(str) {
var t = document.createTextNode(str);
}
<input type="button" onClick="makeText('My new text element!');">That creates it...but you need an object to add it to...like a SPAN or P or DIV or whatever....
Hi,
I didnt understand what you said,Iam new to Internet Technology.What I want is to generate text boxes on a click of a button using Javascript.Pls give me reply in detail...Thx
Regards
Avis..
adios
10-11-2002, 06:44 PM
Pretty basic - but, not much time here...
<html>
<head>
<title>untitled</title>
<script type="text/javascript" language="javascript">
function appendTextInput(formObj) {
if (typeof appendTextInput.count == 'undefined') appendTextInput.count = 1;
if (document.getElementById) {
var txtCtrl = document.createElement('input');
var feed = document.createElement('br');
txtCtrl.setAttribute('type','text');
txtCtrl.setAttribute('id','text' + appendTextInput.count++);
txtCtrl.setAttribute('size','12');
txtCtrl.setAttribute('value',txtCtrl.id);
formObj.appendChild(feed);
formObj.appendChild(txtCtrl);
} else if (document.all) {
var HTML = '<br /><input type="text" size="12" name="text' + appendTextInput.count;
HTML += '" value="text' + appendTextInput.count++ + '">';
formObj.insertAdjacentHTML('beforeEnd', HTML);
}
}
</script>
</head>
<body>
<form name="builder">
<input type="button" value="New Text Box" onclick="appendTextInput(this.form)">
<br />
</form>
</body>
</html>
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.