Log in

View Full Version : Adding Text Into Textbox with JavaScript



magicyte
07-28-2008, 09:18 PM
How is it possible to put text into a textbox using JavaScript?

-magicyte

Jas
07-28-2008, 09:26 PM
Adding text to a textbox?! Impossible!

Just joking. It's completely possible, and very easy to do.

Method 1: Using an ID


function add(text){
var TheTextBox = document.getElementById("Mytextbox");
TheTextBox.value = TheTextBox.value + text;
}


Method 2: using the form:


function add(text){
var TheTextBox = document.forms[0].elements['field_name']; //I think that's right, haven't done it in a while
TheTextBox.value = TheTextBox.value + text;
}

Nile
07-28-2008, 09:26 PM
Here you go magicyte:

<script type="text/javascript">
function insertText(val,e){
document.getElementById(e).innerHTML+=val;
}
</script>
<textarea id="textIns"></textarea><br />
<a href="javascript:insertText('Hello ','textIns');" onClick="void(0)">Insert 'Hello'</a><br /><a href="javascript:insertText('GoodBye ','textIns');" onClick="void(0)">Insert 'GoodBye'</a>


Jas beat me :). I personally thing that my idea is more convenient, though.

Jas
07-28-2008, 09:32 PM
Nile: heh. Cross post :)

Magicyte: . . . so what was the point of your post?

magicyte
07-28-2008, 09:34 PM
I wuz bored and I wanted to see if anyone thought that I was that dumb.

-magicyte

Nile
07-28-2008, 09:35 PM
Using the .value attribute is easy. Just do this: document.getElementById('cool').value = 'po'; <input type="text" id="cool">

Any other .value attribute comments?

-magicyte

Yeah, why'd you update your post?

Jas
07-28-2008, 09:35 PM
Please don't put up posts like that again. If you have nothing else to do, start a topic in "The Lounge."

EDIT: And in response to your edit of the last post, not knowing that does not make one dumb. There are many beginners that use this website looking for simple answers.

magicyte
07-31-2008, 04:47 PM
Yeah. I'm sorry. I do think I should be put in Forums Jail.

-magicyte

P.S. {:-(

Jas
07-31-2008, 07:04 PM
I do think I should be put in Forums Jail.
So you'll come quietly, then? j/k :D

osirix
03-15-2010, 10:42 PM
Sorry if i sound stupid but is it possible to pass a JS variable into the
text value

e.g. var new ="somethign";
<a href="javascript:insertText(' var new ' ,'user_location');" onClick="void(0)">Insert 'Hello'</a>

so when you click it

somethign will be displayed in the box instead of the var new text being displayed

i want to out put the contents of the variable not the name of it
thanks in advance

sendmefree
08-18-2010, 09:03 AM
Check this scripts. Its a very easy to use http://www.dynamicdrive.com/forums/showthread.php?t=39234

jscheuer1
08-18-2010, 09:45 AM
Sorry if i sound stupid but is it possible to pass a JS variable into the
text value

e.g. var new ="somethign";
<a href="javascript:insertText(' var new ' ,'user_location');" onClick="void(0)">Insert 'Hello'</a>

so when you click it

somethign will be displayed in the box instead of the var new text being displayed

i want to out put the contents of the variable not the name of it
thanks in advance


<script type="text/javascript">
var newtext = "something";
</script>


<a href="#" onclick="insertText(newtext ,'user_location'); return false;">Insert Something</a>