We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Javascript: prototype, form is undefined

JianJunShen
JianJunShen asked
on
Medium Priority
835 Views
Last Modified: 2012-05-07
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

Comment
Watch Question

b0lsc0ttIT Manager
CERTIFIED EXPERT

Commented:
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

Author

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.
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Hi: mplungjan:

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

Juhani
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

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?
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks a lot for mahome and mplungjan. Best regards, Juhani

Author

Commented:
The answer is excellent. I appreciate a lot for these two experts.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.