Page 1 of 1

Accessing form field values

Posted: Fri Jun 29, 2007 3:56 pm
by Benjamin
Can someone tell me the best way to access form field values when they have assigned array indexes?

For example:

Code: Select all

<form name="bar" method="post" action="#">
  <input type="checkbox" name="foo[11]" onClick="is_checked(11);" value="checked" />
  <input type="checkbox" name="foo[12]" onClick="is_checked(12);" value="checked" />
  <input type="checkbox" name="foo[13]" onClick="is_checked(13);" value="checked" />
  <input type="checkbox" name="foo[14]" onClick="is_checked(14);" value="checked" />
</form>
So then I want to test if the value is checked..

Code: Select all

function is_checked(int_index)
{
    if (document.bar.foo[ind_index].checked != true)
    {
        alert('foo[' + int_index + '] is checked.');
    } else {
        alert('foo[' + int_index + '] is NOT checked.');
    }
}
I don't get any errors, but it doesn't work either.

Posted: Fri Jun 29, 2007 4:17 pm
by Gente

Code: Select all

function is_checked(int_index)
{
    if (document.bar.elements['foo['+int_index+']'].checked != true)
    {
        alert('foo[' + int_index + '] is NOT checked.');
    } else {
        alert('foo[' + int_index + '] is checked.');
    }
} 

Posted: Fri Jun 29, 2007 4:42 pm
by Benjamin
[s]I don't know if it makes any difference, But I am also trying to get and set field values using that method.

I tried the syntax you posted above. There are no errors but it is not working either.[/s]

Nevermind, it's working great. Thank you very much.