473,395 Members | 1,383 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Using Javascript to change a value in a textbox.

Ant
Ok, so I have a regular input textbox on my webpage and I'm trying to work
out how to make it so when the user presses a button the the textbox holds
the value of a variable. I just can't work out how to do this.

any help much appreciated
Jul 23 '05 #1
2 125377
Ant wrote:
Ok, so I have a regular input textbox on my webpage and I'm trying to work out how to make it so when the user presses a button the the textbox holds the value of a variable. I just can't work out how to do this.

any help much appreciated


That textbox - like all the HTML elements on your webpage (and many
other items as well) is represented in the (java)scripting environment
by an object. This is a software construct that provides the interface
('control panel') for programming the textbox itself. So...first, you
need to get a legitimate reference (something like a pointer) to that
object.

HTML:

<form name="a_form">
<input type="text" id="a_tbox" name="a_tbox" value="" />

JS:

var tbox = document.getElementById('a_tbox'); //uses id
....or
var tbox = document.forms['a_form'].elements['a_tbox']; //uses name

There are numerous other ways, but these are probably the commonest,
cross-browser. That (INPUT) object you just found has a .value property
- basically a read/write variable 'attached' to it, which holds the
currently displayed text string. To alter it, just assign that
variable:

var txt = 'some text';
var tbox = document.getElementById('a_tbox');
if (tbox)
{
tbox.value = txt;
}

Now all you need is to trigger the process from an event handler, in
this case the 'onclick' handler of the button you're using:

<input type="button" name="btn" value="write it" onclick="writeit()" />

What's 'writeit()'? It's the function where you 'stored' the above
instructions (statements):

function writeit()
{
var txt = 'some text';
var tbox = document.getElementById('a_tbox');
if (tbox)
{
tbox.value = txt;
}
}

Declare the function in a <script></script> block in the <head>er of
your document. It must be in memory before the button is rendered
(visible) to avoid errors.

Sorry if that's a bit pedantic, but you sound like you're just learning
this stuff. Good luck !

Jul 23 '05 #2
Ant

"RobB" <fe******@hotmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Ant wrote:
Ok, so I have a regular input textbox on my webpage and I'm trying to

work
out how to make it so when the user presses a button the the textbox

holds
the value of a variable. I just can't work out how to do this.

any help much appreciated


That textbox - like all the HTML elements on your webpage (and many
other items as well) is represented in the (java)scripting environment
by an object. This is a software construct that provides the interface
('control panel') for programming the textbox itself. So...first, you
need to get a legitimate reference (something like a pointer) to that
object.

HTML:

<form name="a_form">
<input type="text" id="a_tbox" name="a_tbox" value="" />

JS:

var tbox = document.getElementById('a_tbox'); //uses id
...or
var tbox = document.forms['a_form'].elements['a_tbox']; //uses name

There are numerous other ways, but these are probably the commonest,
cross-browser. That (INPUT) object you just found has a .value property
- basically a read/write variable 'attached' to it, which holds the
currently displayed text string. To alter it, just assign that
variable:

var txt = 'some text';
var tbox = document.getElementById('a_tbox');
if (tbox)
{
tbox.value = txt;
}

Now all you need is to trigger the process from an event handler, in
this case the 'onclick' handler of the button you're using:

<input type="button" name="btn" value="write it" onclick="writeit()" />

What's 'writeit()'? It's the function where you 'stored' the above
instructions (statements):

function writeit()
{
var txt = 'some text';
var tbox = document.getElementById('a_tbox');
if (tbox)
{
tbox.value = txt;
}
}

Declare the function in a <script></script> block in the <head>er of
your document. It must be in memory before the button is rendered
(visible) to avoid errors.

Sorry if that's a bit pedantic, but you sound like you're just learning
this stuff. Good luck !


That's a really informative answer, and has helped me absolutely loads,
thank you very much indeed!!
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: ED M. | last post by:
Hello all...I'm new to the board. I have a problem that I hope someone here might be able to solve for me. I am doing some clientside validation using Javascript. The text I am testing is...
9
by: nail | last post by:
Hi. So I have a default.aspx page and 3 WebUserControls In the WebUserControl1, I have on TexBox, and in the default.aspx page I have a Button control. On the click event of the default.aspx page...
2
by: Richard | last post by:
I have a textbox and an HtmlInputButton control on a Webform for an Intranet app. When the user clicks the button, it is supposed to add today's date to the textbox using JavaScript on the client...
1
by: c.verma | last post by:
I am not able to hide a href element using javascript. Here is my code written on aspx page. On the click of "OK" button, I want to hide href element. But I am getting message: Object required....
2
by: rockdale | last post by:
Hi, All: I have a datagrid that embedded 3 textbox using TemplateColumn. When the user key in value in the first textbox, I need to check to see if this value is between the the values in other...
4
by: Goofy | last post by:
Hi everyone, My question is related to making a form submit using javascript. Here is my scenario. I have a form, which includes a user control. The user control has a search button and a...
2
by: rockdale | last post by:
Hi, I have a edit interface with textbox, checkbox groups ..etc, users can edit /check/uncheck these controls, these edit controls are seperate to different zones, and there has one save button...
3
by: viral123 | last post by:
Hi all, does any one know how to get the functionality of get focus and lost focus using ASP.Net like VB.Net I want to change the textbox back ground color when it has the focus. I used...
0
by: baburmm | last post by:
Hai, I want to get the footer textbox value in gridview using javascript any one help me Regards, Babu.K
0
by: baburmm | last post by:
hello friends i want to get the footer row textbox value of the gridview using javascript for validating it. how can i get it is there any coding pls help me Thanks in advance, Regards. Babu.K
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.