Changing the value of a form element

D

Derek Fountain

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...?
 
T

Tim Streater

Derek Fountain said:
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.
 
V

VK

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;
 
R

Randy Webb

VK said the following on 2/19/2007 4:08 PM:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top