1

I've got code:

function check($ile,$co,$gdzie)
{
  if (document.forms.form.$co.value.length >= $ile){
    document.forms.form.$gdzie.focus();
  }
}

And it isn't working. Error (in Google Chrome checked):

Uncaught TypeError: Cannot read property 'value' of undefined script.js:39
check script.js:39
onkeyup

And it's my html code:

<p><label><?php echo TEL; ?>: <input type='text' name='tel' size='9' maxlength='9' onkeypress="keyPressNumb(event);" onKeyUp="check(9,'tel','pesel');"></label></p>
<label><?php echo PESEL; ?>:<span class='red'>*</span> <input type='text' name='pesel' size='11' maxlength="11" onKeyUp="check11();" onkeypress="keyPressNumb(event);"></label></p>
1
  • 3
    Try document.forms.form[$co].value.length, as you can not use a variable when accessing a value of an object via dot notation.
    – mAu
    Aug 12, 2012 at 19:26

1 Answer 1

1

Try document.forms.form[$co].value.length, as you can not use a variable when accessing a value of an object via dot notation.

In your code, js expects a property $co in the object document.forms.form. When you want to access a property via a variable, you have to use brackets to access the property.

document.bar = "hello world";

var foo = "bar";
document.foo // undefined, because document has no property "foo"
document[foo] // "hello world"
1
  • @Quarties You're welcome. Please read the faq, as you appear to be new to stackoverflow. If you found this answer helpful, consider approving it.
    – mAu
    Aug 12, 2012 at 20:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.