Solved

Javascript: prototype, form is undefined

Posted on 2009-07-02
13
814 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
  • 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 380 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
 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 120 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 380 total points
ID: 24761812
No it isn't. Store it in a hidden field.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 120 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 380 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 120 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now