tags:

views:

94

answers:

6

I am getting an error (document.my_formm.fieldName.value is null or not an object) from the below code:

<html>
  <head>
    <title>(Type a title for your page here)</title>

    <script language=JavaScript>
    function check_length(my_formm,fieldName)
    {
      alert(fieldName);
      alert(document.my_formm.fieldName.value);
    }
    </script>
  </head>
  <body>
    <form name=my_form method=post>
      <input type="text" onChange=check_length("my_form","my_text"); name=my_text rows=4 cols=30 value="">
      <br>
      <input size=1 value=50 name=text_num> Characters Left
    </form>
  </body>
</html> 
+2  A: 

In your javascript you have referred to the form as 'my_formm' i.e. you have an extra 'm' at the end which is not present in the HTML, this could be your problem.

murdoch
thats y i passing the form name as parameter in the check_length() method
raja
but you are not referencing to the variable passed into the function because you have placed 'document.' in front of my_formm, also, you pass the parameter in as a string not the form its self, you want to remove 'document.' and change your method call to onchange="check_length(this,"my_text")"
murdoch
is it possible by passing form name instead of this?
raja
Yes but there isn't any need as 'this' already refers to the form you need, if you must could give the form an id instead of a name then inside your function call 'document.getElementByID(my_formm)'
murdoch
A: 

Just do onChange=check_length(this)

and in your function

function check_length(element)
     {
       // element points to the element in question
       // element.form points to the form if you need it
       alert(element.value); 
     }
Gaby
My requirement is need to pass the form name and field name not using this.. Using that i should do the validation
raja
@raja, you can get those from the element... `element.form.name` and `element.name`.
Gaby
+6  A: 

Your check_length function is using variables to identify the form and field names, however, by using dot notation, you are referring to a element of document named my_formm. When you are are using variable names, you should use the bracket notation instead:

function check_length(my_formm,fieldName)
{
  alert(fieldName); 
  alert(document[my_formm][fieldName].value); 
}

Also, you should really quote attributes in your input:

<input type="text" onKeyPress="checkCompanyName();" onChange="check_length('my_form', 'my_text');" name="my_text" rows="4" cols="30" value="">
Daniel Vandersluis
+1 Much more succinct than my answer. Deleting mine.
Vivin Paliath
The standard way (DOM0) to access forms and form elements is: `document.forms[my_formm].elements[fieldName]`
CMS
+1  A: 

Why does your JavaScript method take in that first parameter if it never uses it?

JB King
A: 

In general it would be nice to write WHAT error are you getting...

anyhow checkCompanyName is not defined in the code you wrote.

Also you're passing two strings as variable, they do not have properties...

A better way to do this:

<script type="text/javascript">
function checkLength()
    {
    inp = document.getElementById("myInput");
    len = document.getElementById("len");
    len.value = inp.value.length;
    }
</script>

<input id="myInput" onkeyup="checkLength()" />
<input id="len" />

EDIT AFTER COMMENT:

<script type="text/javascript">
function checkLength(inputname, lenname)
    {
    inp = document.getElementById(inputname);
    len = document.getElementById(lenname);
    len.value = inp.value.length;
    }
</script>

<input id="myInput" onkeyup="checkLength('myInput', 'len')" />
<input id="len" />
nico
Hi:My requirement is need to pass the form name and field name.. Using that i should do the validation
raja
@raja: see my edited answer
nico
A: 

document.my_formm looks for a form named my_formm. You need to use the associative array sintax instead, like document[my_formm], which will pass the value in my_formm at runtime, rather than looking for a property in the document object called my_formm (which doesn't exist).

jasongetsdown