sven30
sven30

Reputation: 583

Working with multiple CKEditors using checkDirty() to alert user if content has changed before leaving page

I have this code which works great for one CKEditor textarea field. It alerts users that their content has been changed before leaving the page. But some pages have multiple CKEditors and I was wondering how I might acknowledge them all using this JavaScript code.

function beforeUnload( e )
{
    if ( CKEDITOR.instances.description.checkDirty() ) 
        return e.returnValue = "You will lose the changes made in the editor."; 

}

if ( window.addEventListener )
    window.addEventListener( "beforeunload", beforeUnload, false );
else
    window.attachEvent( "onbeforeunload", beforeUnload );

Description is the name of the textarea, but some have other textarea names like products_short_description[1] or products_description[1]. I need to find a way to integrate all CKEditor names into this code so if someone makes a change on one of the 2 or 3 editors it alerts them accordingly.

Upvotes: 1

Views: 1078

Answers (1)

Reinmar
Reinmar

Reputation: 22023

Just iterate all instances like this:

function beforeUnload( evt ) {
    for ( var name in CKEDITOR.instances ) {
        if ( CKEDITOR.instances[ name ].checkDirty() )
            return evt.returnValue = "You will lose the changes made in the editor."; 
    }
}

Upvotes: 4

Related Questions