473,387 Members | 1,530 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,387 software developers and data experts.

Changing the value of a form element

Given a form, defined like this:

<form action="today.php" method="post" id="continue-form">

i.e without a 'name', can I change the value of an element in it from
script?

If I give it a name ('Form1') this works nicely:

document.Form1.element.value = 1;

but without the 'name' I can't find a way to do it.

The HTML is being generated by a content management system and it's less
than trivial to update so it names the form. If that's the only way
forward I can do it, but I thought I'd check first to see if there's
another way, perhaps by using that 'id' attribute...?

--
Derek Fountain on the web at http://www.derekfountain.org/
Feb 19 '07 #1
6 13701
Derek Fountain wrote:
i.e without a 'name', can I change the value of an element in it from
script?
If I give it a name ('Form1') this works nicely:
document.Form1.element.value = 1;
It's best to avoid this syntax.

See: http://www.javascripttoolbox.com/bestpractices/#forms

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Feb 19 '07 #2
In article <45***********************@news.zen.co.uk>,
Derek Fountain <no****@hursley.ibm.comwrote:
Given a form, defined like this:

<form action="today.php" method="post" id="continue-form">

i.e without a 'name', can I change the value of an element in it from
script?

If I give it a name ('Form1') this works nicely:

document.Form1.element.value = 1;

but without the 'name' I can't find a way to do it.

The HTML is being generated by a content management system and it's less
than trivial to update so it names the form. If that's the only way
forward I can do it, but I thought I'd check first to see if there's
another way, perhaps by using that 'id' attribute...?

This is better:

document.forms['Form1'].element.value = 1;
If push comes to shove you could do:

document.forms[0].element.value = 1;

assuming its the first (or only) form. But the first version is better
practice I would say.

Personally I would change the CMS.
Feb 19 '07 #3
Matt Kruse wrote:
Derek Fountain wrote:
>i.e without a 'name', can I change the value of an element in it from
script?
If I give it a name ('Form1') this works nicely:
document.Form1.element.value = 1;

It's best to avoid this syntax.

See: http://www.javascripttoolbox.com/bestpractices/#forms
Very useful link, thanks.

--
Derek Fountain on the web at http://www.derekfountain.org/
Feb 19 '07 #4
This is better:
>
document.forms['Form1'].element.value = 1;
Matt's link mentions that with XHTML forms without names are fine as
long as they have an ID. getElementByID() is then the answer, and that
seems to work for me. Thanks for your help though. :o)

--
Derek Fountain on the web at http://www.derekfountain.org/
Feb 19 '07 #5
VK
On Feb 19, 11:18 pm, Derek Fountain <nom...@hursley.ibm.comwrote:
Given a form, defined like this:

<form action="today.php" method="post" id="continue-form">

i.e without a 'name', can I change the value of an element in it from
script?
To keep using DOM 0 methods:

document.forms[0].elements['elementName'].value = NewValue;

That presumes of course that your form is always the first one in the
document flow - or that it has some fixed position in the flow to use
some known index for forms collection.

With DOM 1:

document.getElementById('continue-form').elements['elementName'].value
= NewValue;

Feb 19 '07 #6
VK said the following on 2/19/2007 4:08 PM:
On Feb 19, 11:18 pm, Derek Fountain <nom...@hursley.ibm.comwrote:
>Given a form, defined like this:

<form action="today.php" method="post" id="continue-form">

i.e without a 'name', can I change the value of an element in it from
script?

To keep using DOM 0 methods:

document.forms[0].elements['elementName'].value = NewValue;

That presumes of course that your form is always the first one in the
document flow - or that it has some fixed position in the flow to use
some known index for forms collection.
document.forms['formIDnotNAME'].elements['elementNAMEnotID'].value;

Now, it doesn't make a difference, it will work as long as the form has
an ID and the element has a NAME.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 19 '07 #7

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

Similar topics

1
by: Matt | last post by:
Hi group, Here's a problem I've been trying to solve for the past several weeks. I have the standard WebBrowser object added to a form so it can access websites, and I have it load a default...
7
by: Hal Vaughan | last post by:
I have a sample script from a book ("Beginning JavaScript" by Paul Wilton) that removes or adds a choice to a <SELECT> element. The <FORM> is form1 and the <SELECT> is theDay. The example uses...
4
by: multimatum2 | last post by:
Hello, I need to enable/disable input text forms... But... I need to have the same style (color...) in both modes.. Could you help me ? Thanx a lot A small sample... ...
8
by: Ryan | last post by:
I have a hidden field as such: <INPUT TYPE=\"hidden\" name=xmlfield > I have a button that i want to use to call a function to change the value: <INPUT TYPE=submit VALUE="Display XML"...
8
by: horos | last post by:
hey all, Ok, a related question to my previous one on data dumpers for postscript. In the process of putting a form together, I'm using a lot of placeholder variables that I really don't care...
7
by: Stefan Finzel | last post by:
Hi, is there a way to change the display property on Windows Mobile 2003 SE Mobile/Pocket Internet Explorer? See following example. Please note: visibilty property has the same problem. Is...
2
by: Urs Vogel | last post by:
Hi When using XmlDocument, I can create nodes and attributes as I like. What I didn't achieve is changing the Value of a node (created with createElement), it claims that it's the wrong node...
2
by: yawnmoth | last post by:
Say I have two input elements and that I wanted to make it so that when the first ones input was what it should be, the focus would automatically be shifted to the next input element. ie....
2
by: Gary Dale | last post by:
I have a form with a pull-down list with six options, each of which has a value set. The value is the e-mail account name (without the domain) of a group while the displayed value is the full name...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.