david-mark-llc / jessie

Javascript cross-browser library builder enabling (real) progressive enhancement
http://jessie.herokuapp.com
39 stars 13 forks source link

implement hasFormChanged #252

Open web-bert opened 10 years ago

web-bert commented 10 years ago

Is it possible cross-bowser to implement a function to tell you if the form has changed? Is this something we should have at all?

An example (which obviously doesn't account for all input types):

function hasFormChanged( form ){

        var elements = form.elements,
            element,
            i = 0,
            hasChanged = false;

        while( ( element = elements[ i++ ] ) ){

            switch( element.type ){
                case 'text':
                case 'textarea':
                    //console.log( 'text: ' + element.defaultValue + ' === ' + element.value, element.defaultValue === element.value );
                    hasChanged = ( element.defaultValue !== element.value );
                break;
                case 'select-one':
                    //console.log( 'select-one: ' + element.selectedIndex + ': ' + element.options[ element.selectedIndex ], element.options[ element.selectedIndex ].defaultSelected );
                    hasChanged = !element.options[ element.selectedIndex ].defaultSelected;
                break;
                case 'checkbox':
                    //console.log( 'checkbox: ' + element.checked +' === '+ element.defaultChecked, element.checked === element.defaultChecked );
                    hasChanged = ( element.checked !== element.defaultChecked );
                break;
            }

            if( hasChanged ){
                break;
            }
        }

        return hasChanged;
    }
adamsilver commented 10 years ago

Yes. Can detect existence of properties and then create that function. Can be made up of smaller functions for each e.g. hasTextBoxChanged, hasSelectChanged On Apr 7, 2014 9:20 AM, "web-bert" notifications@github.com wrote:

Is it possible cross-bowser to implement a function to tell you if the form has changed? Is this something we should have at all?

An example (which obviously doesn't account for all input types):

function hasFormChanged( form ){

    var elements = form.elements,
        element,
        i = 0,
        hasChanged = false;

    while( ( element = elements[ i++ ] ) ){

        switch( element.type ){
            case 'text':
            case 'textarea':
                //console.log( 'text: ' + element.defaultValue + ' === ' + element.value, element.defaultValue === element.value );
                hasChanged = ( element.defaultValue !== element.value );
            break;
            case 'select-one':
                //console.log( 'select-one: ' + element.selectedIndex + ': ' + element.options[ element.selectedIndex ], element.options[ element.selectedIndex ].defaultSelected );
                hasChanged = !element.options[ element.selectedIndex ].defaultSelected;
            break;
            case 'checkbox':
                //console.log( 'checkbox: ' + element.checked +' === '+ element.defaultChecked, element.checked === element.defaultChecked );
                hasChanged = ( element.checked !== element.defaultChecked );
            break;
        }

        if( hasChanged ){
            break;
        }
    }

    return hasChanged;
}

Reply to this email directly or view it on GitHubhttps://github.com/rassie/jessie/issues/252 .

web-bert commented 10 years ago

I was thinking it would be easier to account for all future types by detecting the properties. but I'm not sure if it would be robust enough?:

if( typeof element.defaultValue !== 'undefined' ){

    hasChanged = ( element.defaultValue !== element.value );

} else if( typeof element.options !== 'undefined' ){

    hasChanged = !element.options[ element.selectedIndex ].defaultSelected;

} else if( typeof element.checked !== 'undefined' ){

    hasChanged = ( element.checked !== element.defaultChecked );
}
david-mark commented 10 years ago

I'd break it up as some (e.g. text inputs) are trivial (requiring just one rendition), but others (e.g. SELECT elements) will need more thought. For example, a SELECT can have more than one option checked.

On Mon, Apr 7, 2014 at 4:32 AM, web-bert notifications@github.com wrote:

I was thinking it would be easier to account for all future types by detecting the properties. but I'm not sure if it would be robust enough?:

if( typeof element.defaultValue !== 'undefined' ){

hasChanged = ( element.defaultValue !== element.value );

} else if( typeof element.options !== 'undefined' ){

hasChanged = !element.options[ element.selectedIndex ].defaultSelected;

} else if( typeof element.checked !== 'undefined' ){

hasChanged = ( element.checked !== element.defaultChecked );}

Reply to this email directly or view it on GitHubhttps://github.com/rassie/jessie/issues/252#issuecomment-39706614 .