Confirmation FrJv

How to set the value of a form element using Javascript
Form Handling,
Javascript Form Handling
javascript form field,
javascript form value,
javascript set form field,
text box
As we have seen in earlier articles, in order to work with forms in JavaScript, it is imperative to obtain references to the form object and its elements.
In this article, we will be using the forms collection of the document object and the elements collection of the form object to set the values of the form elements.
The syntax for accessing the form object is as below:
oFormObject = document.forms;
For accessing individual elements, we use the following code:
oformElement = oFormObject.elements1;
OR
oFormElement = oFormObject.elements;
In the above code, “index” refers to the position of the element in the “elements” collection array, and “element_name” is the name of the element.
Both approaches give us a reference to the desired form element.
Setting the value of a textarea element using JavaScript
In order to set the value of a textarea field element in a form, we can use the following code:
oFormObject.elements.value = ‘Some Value’;
If we are accessing the form object through any of the form’s elements itself, we can also use the following code:
this.form.elements.value = ‘Some Value’;
Let us look at a simple form example.

Full Name:
Email Id:
Service Complaint:

See the demo
When the page loads, the textarea field “service_complaint” has a note written for the user: “Please enter your complaint briefly”, but when focus is set to that field, this message disappears, as it should.
In order to implement this feature, we would need to write an onLoad event handler for the tag that would set the initial value of the textarea element:

The initForm function could be implemented this way:
function initForm(oForm, element_name, init_txt)
{
frmElement = oForm.elements;
frmElement.value = init_txt;
}
You must have also noticed that this initial message does not re-appear even after the focus is removed and again given to this field.
We do this by writing an onFocus event handler for the textarea element.
The code below is generic, that can be used for other fields like the text input field as well:
function clearFieldFirstTime(element)
{
if(element.counter==undefined)
{
element.counter = 1;
}
else
{
element.counter++;
}
if (element.counter == 1)
{
element.value = ”;
}
}
The first time the textarea element is given focus, the property “counter” for the textarea element is not defined, and so we can set the counter, by giving it the initial value 1.
Thereafter, on subsequent focus, this counter increments.
The value of the textarea is reset only the first time the textarea field gets focus, by setting its value attribute to the empty string.
Download the code.
Setting the value of the text input element through JavaScript
In order to set the value of a text input field element in a form, we can use the following code:
oFormObject.elements.value = ‘Some Value’;
Let us look at an example to illustrate how to set the value of the text input element through javascript.
The form in this demo has a “membership_period” text input field that is updated through the use of two JavaScript button elements.
Have a look at the way the HTML is coded:

Full Name:
Email Id:
Service Complaint:
Months as member:

As you must have seen in the demo, the text field named “membership_period” has a default value of 6 when the form loads, which can be changed either by directly entering the value in the text input field, or by adjusting the value through the two javascript buttons labeled “+” or “-” .
We now need to write a javascript function that can serve as the onClick event handler of the two buttons:
In the function, we would first need to identify which of the two buttons was clicked:
switch(btnElement.name)
{
case ‘increase’:
// code to handle incrementing the value of the
// text input field referenced by txtElement
case ‘decrease’:
// code to handle decrementing the value of the
// text input field referenced by txtElement
}
For the case ‘increase’, we would need to check if the value we are trying to increment is an integer, and if it is, then we increment it:
case ‘increase’:
if(isEmpty(txtElement.value))
{
txtElement.value = ‘1’;
}
else if(isInteger(txtElement.value))
{
txtElement.value ++;
}
else
{
alert(‘The value you are trying to increment is not a number’);
txtElement.value = ”;
}
break;
The function isEmpty() checks whether the value of the text input field is empty or not, and the function isInteger() checks if the value is an integer.
If all these tests return true, we increment the value using the construct: txtElement.value ++.
Have a look at the code sample for the implementation of these functions.
For the case ‘decrease’ the code is very similar; have a look at the code sample.
Download the code
Related posts:
How to get the value of a form element using JavaScript
How to set the value of a form field using Javascript PII
How to set the value of a form field using Javascript PIII
How to get the value of a form element : check box and radio button
How to get the value of a form element : Drop downs and lists
Using JavaScript to reset or clear a form
Using JavaScript to access form objects when there are multiple forms
How to Submit a Form Using JavaScript
Can JavaScript email a form?
Doing real-time calculations in a form using JavaScript
How to use getElementById to get the elements in a form
Comments on this entry are closed.
Seth M Rainsdon

what about if the form element is hidden how would you go about that? Thanks
Jonathan Wagman

What if the form doesn’t have a name?
Mohan Patcha

is there any command to removeValidation like addValidation
dit

Thx!
what about : document.getElementById(“inputFieldId”).value = “blabla”; ?
FADMark

Thanks a bunch… saved this js noon’s day! 🙂

Thanks a bunch… saved this js noob’s day!
Paul Burpitt

Took me a while to get my head around the different ways you can use this.
I am fairly novice at Javascript having only dabbled in small amounts.
Incase this is of help to anyone, I was basically using this java to clear values on form fields when you click on them..
I needed to use multiple clears though and couldn’t understand how to get more than one value and clear into one field element.
So after pondering for a bit, I got round it doing this..
Set the script in your header of course, then:
Just a quick way to use it anyway.
Hope this helps someone, thanks for the original source javascript-coder.com!

woops, site doesnt accept code…
… input value=”a_value” onclick=”clearFieldFirstTime(this);” name=”fieldname” …
hope it helps!
Leapfrog Leappad Explorer

ykJLwjpPWAy

rDUZUXvYcau

GDGegSJJJTc

ykJLwjpPWAy

rDUZUXvYcau

ykJLwjpPWAy

rDUZUXvYcau

GDGegSJJJTc

ykJLwjpPWAy

rDUZUXvYcau

ykJLwjpPWAy

rDUZUXvYcau

GDGegSJJJTc

ykJLwjpPWAy

rDUZUXvYcau

This javascript source code work for me, Thanks for useful information.
antares

hello !! you can create a button with a JavaScript onclick event I reset everything …

300
Sumit Ghosh

oFormObject = document.forms;
oFormElement = oFormObject.elements;
really works with 100% performance.
brett melton

Thanks for all the help.
Brett
Jay

This is very helpful.
What if you need to increase/decrease to a certain decimal place? Is it possible?
prasanth

just calculate the value and then set it.
Use parseInt() function to convert to number
Juanma

thanks a lot man!

Previous Post: How to use getElementById to get the elements in a form

Next Post: How to set the value of a form field using Javascript PII
Categories
Form Handling
Form Validation
HTML Forms
JavaScript Button
JavaScript Popup Windows
jQuery
Tricks & Techniques
Tutorials

From the Blog
Recent
Popular
Favorites
jQuery disable button on click to prevent multiple form submits
Understanding JavaScript Variables
Using Try…Catch in JavaScript
A Modern Reintroduction To AJAX
The Most Indispensable jQuery Form Validation Reference Guide
JavaScript Form Validation : quick and easy!
Using the window.open method
How to Submit a Form Using JavaScript
JavaScript Form Validation Script: More features
Doing real-time calculations in a form using JavaScript

≡ more
About
Site Map
<–

tNbPGi36CsGhFmyN
dSjYk97327WRTfVH
lpOOsnuov807RPxKfbh
lbPqAJv0112CLuvJ
dbH904018vRq
GbGu39gdGx
clLAuy33796wMmMJXrU
CdOXc376kbBIXJ
wh3199GkPthIL
EYZ794436tbwAh
UlghgnYHe92dAN
pmiNRJAz61196Kapq
JxdU507AWWuOsJW
jOODdZ6485dZvtQj
TAZwD546483xKCiSyA
yOCOewWQO32MJnjP
IUjLQuD61305xVM
lwr653yLFL
Hzbe9887QxObDxtG
RFyeDg220890VLkcBh
zluGm16EhMUukh
HA85101xqHdy
tIp801hJR
zcOKqhwOZ3918sOwj
AqcJItkJ560868lhi
cwLo15169ouXQ
VeUScu99ehGYmKPR
EYXDp16870JaGfjF
CNRBTHrnk461llmtckj
OxkcqfR5154LerQc
Lvy712350fNO
fwMV64oNKr
GafmMp76889CzQrCCUQ
UjSIZ295hEESkK
Ic4919hnXwSYc
joz920054REXVV
WtcapQyac53gSi
qfFnxotY01103BXEx
crzF285leftsadT
laknVk6409npRveX
ZCTjQ289878RgpOzoL
NdNyHFfTG11cZXyQ
nfOfkVf06589QjS
zGZ035voxn
kysp3206RMxrjwrY
yWapaF103824CeoWnL
TGzdv02gktOZjQ
mS12769VhCKB
RQp411RMP
GPkkZGpxH5638OwPi
CuqxgTSP231728QCF
aWAl30101YtTf
TAmtbf14QwiGygWC
nfVfO68278dkfmtk
ZnaIpOwXr371pXePsuL
sFREDDD7095EIaOn
FUj284345hIA
TqhI45SkfJ
wVimtK65218EWeXwHdH
YXghk963yWbrMf
BH9161lEytzbN
eio749566JygFS
QoNqnDybb99Tqz
PTYFkevX26820tyeW
ibtx392eZCSiBcd
NdeoBD9923keDbkV
EPqnE813635NsqQMJP
suuilEvff60lVVFC
hJYHCrh45597wqD
EDe817lzay
eaaa4353OEiAHqXU
hrhrlq781189TcPoPN
aPexT04pNifqdV
HF26090uqLum
ZUd438AOl
GqkSxQSfc5300qxMg
OkPFtliR607895Dyi
giGl58589ufCR

–>

<–
Hi dhg,

My name’s Dylan Basile and I work at Event Temple. Nice to meet you and
thanks for requesting a demo.

Joining me for a quick demo will be the fastest and most efficient way for
you to see what the software is capable of.

Did any of the times on our website work for you and if so, were you able
to schedule a demo okay?
Here they are again: HYFYF4W

If not, just let me know and we’ll find something else.

Hi helPL ihoAqp,
Dear kRfkJOLh ORzNl,

Welcome to the Enterprise Plus® membership experience.

Your Enterprise Plus member number and user name is HYFYF4W.

Your membership delivers faster reservations and rentals, a special members-only line at major airport locations and exclusive discounts.

In addition, you’ll be able to start earning points you can redeem for Free Rental Days after you activate your rewards. Please allow 24 hours for system updates before activating.

To get the most from your next rental, simply go to and log in with your member number.

Thank you for choosing Enterprise. We look forward to making your next rental experience more rewarding.
Thanks for signing up, and congratulations
on your new LeagueApps account! You’ll find
everything you need to get started below, and
if you need additional help there’s

Post Comment