?
Solved

Javascript: prototype, form is undefined

Posted on 2009-07-02
13
Medium Priority
?
820 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

0
Comment
Question by:JianJunShen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
  • +1
13 Comments
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 24761302
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
 

Author Comment

by:JianJunShen
ID: 24761339
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
 
LVL 10

Assisted Solution

by:mahome
mahome earned 1520 total points
ID: 24761501
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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 480 total points
ID: 24761663
the question remains WHY do you want to put the serialised form into the ID's nodevalue?
0
 

Author Comment

by:JianJunShen
ID: 24761732
Hi: mplungjan:

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

Juhani
0
 
LVL 10

Assisted Solution

by:mahome
mahome earned 1520 total points
ID: 24761812
No it isn't. Store it in a hidden field.
0
 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 480 total points
ID: 24762040
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
 

Author Comment

by:JianJunShen
ID: 24762111
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
 
LVL 10

Accepted Solution

by:
mahome earned 1520 total points
ID: 24763231
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
 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 480 total points
ID: 24765444
Drat. I wrote similar code and forgot to submit before I shut down the pc :((
0
 

Author Comment

by:JianJunShen
ID: 24766274
Thanks a lot for mahome and mplungjan. Best regards, Juhani
0
 

Author Closing Comment

by:JianJunShen
ID: 31599093
The answer is excellent. I appreciate a lot for these two experts.
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question