Javascript to indicate form content has been edited?

M

M Wells

Hi All,

I'm wondering if anyone can point me to some JavaScript code I can
drop into a page to respond to an attempt to click on specific links
if content in textarea on a form has been changed but not submitted?

In essence, if someone starts editing content in a textarea on the
form, then clicks on one of several specific links, I'd like a box to
come up asking them if they want to lose their edits or submit the
form first.

Any help will be much appreciated!

Much warmth,

Murray
http://www.planetthoughtful.org
Building a thoughtful planet,
one snide comment at a time...
 
F

Fred Oz

M said:
Hi All,

I'm wondering if anyone can point me to some JavaScript code I can
drop into a page to respond to an attempt to click on specific links
if content in textarea on a form has been changed but not submitted?

In essence, if someone starts editing content in a textarea on the
form, then clicks on one of several specific links, I'd like a box to
come up asking them if they want to lose their edits or submit the
form first.

Presumably, the user enters some text to the text area and submits the
form. You return a page with the text area and the submitted text
inside it.

If the user edits the submitted text, you want to know about it. Is
that right?

Put a copy of the submitted/returned text into a hidden text area:

<textarea ... style="display: none;" ...>the submitted text</textarea>

Then compare the text in the displayed textarea with the hidden one
when the user submits the form a second time. You can add one for each
control you want to check on - make sure you deal with special
characters properly when comparing the textarea values.

Fred.
 
M

Matt Kruse

M said:
I'm wondering if anyone can point me to some JavaScript code I can
drop into a page to respond to an attempt to click on specific links
if content in textarea on a form has been changed but not submitted?

In my functions at http://www.javascripttoolbox.com/validations/ there is a
function called "isFormModified" which checks a form for any changes from
when it was initially loaded.

So then you could do,

<a href="newpage.html" onClick="if(isFormModified(document.forms[0])){return
confirm('Form has been changed! Are you sure you want to leave the
page?');}else{return true;}">Link</a>

(not tested, change form reference as necessary, beware of line wrap)

Hope that helps,
 
R

Randy Webb

M said:
Hi All,

I'm wondering if anyone can point me to some JavaScript code I can
drop into a page to respond to an attempt to click on specific links
if content in textarea on a form has been changed but not submitted?

Use the onchange event handler of the textarea to set a flag variable.
Use the onclick of the links to check that variable:

<textarea onchange="hasChanged=true">
<a href="..." onclick="if(hasChange)........">
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top