Javascript: prototype, form is undefined

I have form, when user modify something, it will popup"Are you sure you want to close? Unsaved changes will be lost". If it does not modify something, page would be closed silently. The whole page is as follows. I am using prototype.

The problem is at following part of codes:

<script type="text/javascript">
      $('selfregform').attributes['id'].nodeValue=$('selfregform').serialize();
</script>

The firebug says $('selfregform') is undefined. I change to document.forms['selfregform'], it complains document.forms['selfregform'] is undefined. How to overcome this problme?
<html>
<head>
<script type="text/javascript" src="prototype/prototype.js"></script>
 
<script type="text/javascript" src="toolportal_edit.js"></script>
<script type="text/javascript">
	$('selfregform').attributes['id'].nodeValue=$('selfregform').serialize();
 
</script>
 
 
</head>
<body>
<form id='selfregform' action="http://www.hut.fi" method="POST" class="observed-form">
<input type="text" name="aName" value="test" />
<input type="checkbox" name="ignore" />
<input type="submit" name="action" id="close_button" value="Close" onclick="if ($('selfregform').attributes['id'].nodeValue] == $('selfregform').serialize() || confirm('Are you sure you want to close? Unsaved changes will be lost.')) { } $('selfregform').action.value='close';$('selfregform.submit(); return false;"/>
 
</form>
</body>
</html>

Open in new window

JianJunShenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

b0lsc0ttIT ManagerCommented:
JianJunShen,

I don't know Prototype but for the document.forms[] method to work the name attribute needs to be in the form tag.  Right now you just have it with the ID.  Add a name attribute and provide the same value and that second code should work.

Let me know if you have any questions or need more information.

b0lsc0tt
0
JianJunShenAuthor Commented:
I changed to

<script type="text/javascript">
        document.forms['selfregform'].attributes['id'].nodeValue=$('selfregform').serialize();
 
</script>


<form id='selfregform' name=''selfregform' action="http://www.hut.fi" method="POST" class="observed-form">

It has same problem.
0
mahomeCommented:
the form is undefined because the JavaScript is executed in the head before the document is there. Put the script block after the form. Then it will work with the syntax you posted first.
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Michel PlungjanIT ExpertCommented:
the question remains WHY do you want to put the serialised form into the ID's nodevalue?
0
JianJunShenAuthor Commented:
Hi: mplungjan:

I just want to store serialised value. Is it a good way to store it into id?

Juhani
0
mahomeCommented:
No it isn't. Store it in a hidden field.
0
Michel PlungjanIT ExpertCommented:
Why store it in the form at all?
If you submit the form you get the serialised value (assuming GET) anyway
If you need it in script, store it in a javascript variable
0
JianJunShenAuthor Commented:
Well, I need to check whether form has been modified or not? If not, I will submit the form directly, if modified, I will pop up a remind dialog. could a javascript variable handle that?
0
mahomeCommented:
Yes it will do. Here is the modified example:

<html>
<head>
<script type="text/javascript" src="prototype-1.6.0.3.js"></script>
<script type="text/javascript">
var formSerial;
function modifiedAlert() {
	return formSerial == $('selfregform').serialize()
		|| confirm('Are you sure you want to close? Unsaved changes will be lost.');
}
</script>
</head>
<body>
<form id='selfregform' action="http://www.hut.fi" method="POST" class="observed-form">
<input type="text" name="aName" value="test" />
<input type="checkbox" name="ignore" />
<input type="submit" name="action" id="close_button" value="Close" onclick="return modifiedAlert();" />
</form>
<script type="text/javascript">
        formSerial = $('selfregform').serialize();
</script>
</body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Michel PlungjanIT ExpertCommented:
Drat. I wrote similar code and forgot to submit before I shut down the pc :((
0
JianJunShenAuthor Commented:
Thanks a lot for mahome and mplungjan. Best regards, Juhani
0
JianJunShenAuthor Commented:
The answer is excellent. I appreciate a lot for these two experts.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.