Accessing form field values

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Accessing form field values

Post 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.
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post 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.');
    }
} 
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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.
Post Reply