Quarties
Quarties

Reputation: 37

JS - check length function

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>

Upvotes: 1

Views: 353

Answers (1)

mAu
mAu

Reputation: 2018

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"

Upvotes: 1

Related Questions