logo

New Response

« Return to the main article

You are replying to:

    • avatar
    • Campbeln
    • Posted on Thu 29 Jul 2004

    Hey All, <p> I have made some modifications to the code that I'd like to give back. I've cleaned up the code a bit, added way too many comments (as is my coding style) and tweaked a little bit of functionality around the select* processing. If the developer hasn't defined a default value, the code didn't function correclty. <p> If you have any questions, just let me know (search Google for 'campbeln' and you'll find me, I'm the Beastie Boy fan). Be sure to browse the code as the comments should explain the non-default value thing I added.

    http://opensource.campbeln.com/IsElementChanged.js

    <pre> //############################################################ //# Determines if the passed oElement has been changed by the user //# NOTE: This is a heavely modified version of the code at: http://codestore.net/store.nsf/unid/DOMM-4UTKE6?OpenDocument //############################################################ //# Last Updated: July 29, 2004 function isElementChanged(oElement) { //#### Determine the .toLowerCase'd .type of the passed oElement and process accordingly switch (oElement.type.toLowerCase()) { case "text": case "textarea": //#### If the oElement's .value differes from it's .defaultValue, return true if (oElement.value != oElement.defaultValue) { return true; } break;

    case "radio": case "checkbox": //#### If the oElement's .checked value differes from it's .defaultChecked value, return true if (oElement.checked != oElement.defaultChecked) { return true; } break;

    case "select-one": case "select-multiple": var i; var bDefaultValueSpecified = false;

    //#### Traverse oElement's .options to determine if the developer specified any as .defaultSelected for (i = 0; i < oElement.options.length; i++) { //#### If the current .option is set as .defaultSelected, flip bDefaultValueSpecified and set i so we fall from the loop if (oElement.options[i].defaultSelected) { bDefaultValueSpecified = true; i = oElement.options.length; } }

    //#### Traverse oElement's .options for (i = 0; i < oElement.options.length; i++) { //#### If the developer set some .defaultSelected .options if (bDefaultValueSpecified) { //#### If the oElement's .selected value differes from it's .defaultSelected value, return true if (oElement.options[i].selected != oElement.options[i].defaultSelected) { return true; } } //#### Else there are not any .defaultSelected .options set, so if the user has selected something other then the first .option, return true else if (rInput.options[j].selected && j != 0) { return true; } } break; }

    //#### If we make it here, the oElement has not changed, so return false return false; } </pre>

Your Comments

Name:
E-mail:
(optional)
Website:
(optional)
Comment: