JavaScript Runtime Error ( Undefined is null or not an object )

Asked By 400 points N/A Posted on -
qa-featured

I’m setting some page wherein the client can create, delete or modify some records from the database. I’ve included some JavaScript so that when the user chooses one of the option I’ve stated above (create, delete, edit or modify) an alert box will pop-up asking them to confirm that they want to create, delete of modify the file. After I’m done doing the code I try to run it in IE6 but I get JavaScript error message.

Runtime Error:
 
Undefined is null or not an object.
 
Here my code that calls the JavaScript alert box:
 
<form action="/exhibitorhousing/housing-delete.asp" method="post" onSubmit="Delete(this. form); return false"><input type=hidden name="HOUSING_REQUEST_ID" value="<%= HOUSING_REQUEST_ID %>"><input type=hidden name="MODE" value="DELETE"><input type=submit value="Delete"></form>
 
<form action="/exhibitorhousing/housing-create.asp" method="post" onSubmit=Create(this. form); return false"><input type=hidden name="HOUSING_REQUEST_ID" value="<%= HOUSING_REQUEST_ID %>"><input type=hidden name="MODE" value="CREATE"><input type=submit value="Create"></form>
 
<form action="/exhibitorhousing/housing-update.asp" method="post" onSubmit="Update(this. form); return false"><input type=hidden name="HOUSING_REQUEST_ID" value="<%= HOUSING_REQUEST_ID %>"><input type=hidden name="MODE" value="UPDATE"><input type=submit value="Update"></form>
 
My JavaScript code is:
 
function Delete(thisform) {
if (confirm("Are you sure you want to delete this reservation?"))
thisform.submit();
}
 
function Create(thisform) {
if (confirm("Are you sure you want to create this reservation?"))
thisform.submit();
}
 
function Update(thisform) {
if (confirm("Are you sure you want to Update this reservation?"))
thisform.submit();
}
 
Is there someone who can debug and eliminate this irritating error?
SHARE
Best Answer by Berenk guin
Best Answer
Best Answer
Answered By 0 points N/A #108048

JavaScript Runtime Error ( Undefined is null or not an object )

qa-featured

Well Alexia,

There is a small error on your code. The parameter you pass into the functions is undefined.
 
You can check that if you put an alert in each function like as below.
 
function Update(thisform) {
 
if (confirm("Are you sure you want to Update this reservation?"))
 
alert(thisform);
 
}
 
You can see that alert is returning an undefined message for thisform object.
 
You cannot pass the parameter as this.form to functions. It doesn’t mean anything when you use in the form tag itself.
 
<form action="/exhibitorhousing/housing-create.asp" method="post" onSubmit=Create(this.form); return false">
 
There is no object exist as this.form. You have to use the word ‘this’ to pass the form object into each functions like as below.
 
<form action="/exhibitorhousing/housing-create.asp" method="post" onSubmit=Create(this); return false">
 
 
I think you have a misunderstanding of accessing form objects.
 
If you use
 
<input type=submit value="Update" onclick="Update(this.form)">
 
Then there is some meaning because you use it in the input tag.
 
In the code above, the Update function call on onClick event handler of the form input button is being passed as this.form. Here this refers to the input object, and this.form references the form object. You can now make use of this form object in the Update function and access the form and all its elements.
 
Each and every element in a form tag triggers events, based on user action, which can be handled by a proper event handler. For example, the submit button being clicked triggers the onClick event handler. The 'this' keyword in JavaScript language always position to the object that calls a given method. The onClick event handler can be passed ‘this’ self-reference to get hold of a reference to the button element object.
 
Furthermore, in JavaScript language, every element object in a form tag has a property that references to the form object. So, if we pass this.form to the onClick event handler of a form button, this function gets the reference to the form object. This is a straightforward way to access form objects, because the form’s name or id attribute are never directly used. This is the basic option to use even there are multiple forms on a web page.
 
So I corrected your code and you can use it now. Cheers! It is 100% working code.
 
<!– Form elements 1st way –>
 
<form action="/exhibitorhousing/housing-delete.asp" method="post" onSubmit="Delete(this); return false">
 
<input type=hidden name="HOUSING_REQUEST_ID" value="<%= HOUSING_REQUEST_ID %>">
 
<input type=hidden name="MODE" value="DELETE">
 
<input type=submit value="Delete">
 
</form>
 
<form action="/exhibitorhousing/housing-create.asp" method="post" onSubmit=”Create(this); return false">
 
<input type=hidden name="HOUSING_REQUEST_ID" value="<%= HOUSING_REQUEST_ID %>">
 
<input type=hidden name="MODE" value="CREATE">
 
<input type=submit value="Create">
 
</form>
 
<form action="/exhibitorhousing/housing-update.asp" method="post" onSubmit="Update(this); return false">
 
<input type=hidden name="HOUSING_REQUEST_ID" value="<%= HOUSING_REQUEST_ID %>">
 
<input type=hidden name="MODE" value="UPDATE">
 
<input type=submit value="Update">
 
</form>
 
Or else you can use it as
 
<!– Form elements 2nd way –>
 
<form action="/exhibitorhousing/housing-delete.asp" method="post" >
 
<input type=hidden name="HOUSING_REQUEST_ID" value="<%= HOUSING_REQUEST_ID %>">
 
<input type=hidden name="MODE" value="DELETE">
 
<input type=submit value="Delete" onclick="Delete(this.form)">
 
</form>
 
<form action="/exhibitorhousing/housing-create.asp" method="post>
 
<input type=hidden name="HOUSING_REQUEST_ID" value="<%= HOUSING_REQUEST_ID %>">
 
<input type=hidden name="MODE" value="CREATE">
 
<input type=submit value="Create" onclick="Delete(this.form)">
 
</form>
 
<form action="/exhibitorhousing/housing-update.asp" method="post" >
 
<input type=hidden name="HOUSING_REQUEST_ID" value="<%= HOUSING_REQUEST_ID %>">
 
<input type=hidden name="MODE" value="UPDATE">
 
<input type=submit value="Update" onclick="Update(this.form)">
 
</form>
Answered By 0 points N/A #195522

JavaScript Runtime Error ( Undefined is null or not an object )

qa-featured

Good day,

This error can occur if you are trying to use an object which is null. In that code quite a lot of things can return null: $find, findTabByText, getPageView, get_nextTab, get_previousTab etc. I suggest you alert() everything before using it. That way you will find what is null.

Regards, 

Jacksonn Maria

Related Questions