Cannot read property from my document.forms

This question already has an answer here:

  • How to get an input text value in JavaScript

    12 answers

i have this js code on my page:

var mail = 'idEmail';
//function returning the value of an input
function value(input){
  return eval('window.document.Search.'+input).value;

I have this error:

TypeError: Cannot read property 'idEmail' of undefined

Why when i change return eval(‘window.document.Search.’+input).value; by return eval(‘window.document.forms[‘Search’].’+input).value; it’s work?


it’s the same

Because you forgot the forms part:

return eval('window.document.forms.Search.'+input).value;

Note that eval is evil. You might as well do:

return document.forms.Search[input].value;

In your function you are trying to access a property of an undefined object, in fact .Search isn’t a property of window.document but of window.document.forms instead, change it like this:

function value(input){
  return window.document.forms.Search[input].value;


It’s better to avoid using eval, you can get rid of it in your function.