Link to home
Start Free TrialLog in
Avatar of JianJunShen
JianJunShen

asked on

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

Avatar of b0lsc0tt
b0lsc0tt
Flag of United States of America image

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
Avatar of JianJunShen
JianJunShen

ASKER

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.
SOLUTION
Avatar of mahome
mahome
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi: mplungjan:

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

Juhani
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks a lot for mahome and mplungjan. Best regards, Juhani
The answer is excellent. I appreciate a lot for these two experts.