472,791 Members | 1,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,791 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 125336
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
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?

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.