hideously, painfully annoying problem of death

M

mammothman42

WHY, oh WHY, won't my cute little js work? it consistently returns
"initial", that is, it fails to update the hidden field value. PLEASE
help a humbled, annoyed fellow programmer.

var html='<input type="hidden" name="length" value="initial"/>';

var value=1;

document.write(html);
increaseValue();

function increaseValue()
{
value++;
document.getElementsByName('length').value=value;
}

cheers
dave
 
B

Berislav Lopac

WHY, oh WHY, won't my cute little js work? it consistently returns
"initial", that is, it fails to update the hidden field value. PLEASE
help a humbled, annoyed fellow programmer.

var html='<input type="hidden" name="length" value="initial"/>';

var value=1;

document.write(html);
increaseValue();

function increaseValue()
{
value++;
document.getElementsByName('length').value=value;
}

Off of my head, I would say that getElementsByName() (Are you sure there is
such a method? I have never heard about it; there are getElementById and
getElementsByTagName, but I don't know everything...) doesn't return a
single object but a collection of objects (since there can be more than one
element of the same name.

Use either ID and getElementById, or (if I am correct)
document.getElementsByName('length')[0].value
 
M

Michael Winter

On Mon, 4 Oct 2004 12:08:15 +0200, Berislav Lopac

[snip]
Off of my head, I would say that getElementsByName() (Are you sure there
is such a method? I have never heard about it; there are getElementById
and getElementsByTagName, but I don't know everything...)

There is a method called getElementsByName (which I keep forgetting
about...). It is part of the HTMLDocument interface (implemented by the
document object) as described in the DOM HTML Specification.
doesn't return a single object but a collection of objects (since there
can be more than one element of the same name.

....and it does return a collection
Use either ID and getElementById, or (if I am correct)
document.getElementsByName('length')[0].value

Alternatively, if the element is the descendant of a form:

document.forms['formName'].elements['length'].value

Mike
 
I

Ivo

Michael Winter said:
Alternatively, if the element is the descendant of a form:

document.forms['formName'].elements['length'].value

Additionally, naming a form element 'length' is asking for trouble, since

document.forms['formName'].elements.length

is already where the number of elements is stored...
 
B

bruce

WHY, oh WHY, won't my cute little js work? it consistently returns
"initial", that is, it fails to update the hidden field value. PLEASE
help a humbled, annoyed fellow programmer.

var html='<input type="hidden" name="length" value="initial"/>';

var value=1;

document.write(html);
increaseValue();

function increaseValue()
{
value++;
document.getElementsByName('length').value=value;
}

cheers
dave

Personally, I wouldn't use "value" as the name of a javascript
variable. Just asking for trouble somewhere along the way. I'm
surprised it works.
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top