Drupal use jquery to coordinate two cck fields.

I have a module that alters a CCK custom content type and I’m using it to include some jQuery that updates the upper value of paired fields.
In the initial use case the two fields were age range, one for low and one for high. Then I added support for a scenario that uses two date fields as well.
Theoretically the function can be called from anywhere. You only need the ID of the two fields and the type: int or date.

The logic behind the update is simple but optimized for for usability. When it’s invoked, the upper range is checked against the lower range and updated if it’s lower.

I experimented with .blur(), .focus() and .change(). These are shortcuts for jQuery’s .bind() function, which I used to combine blur and change where both are used on one element.

Using jquery is always a learning experiment and I’ve got some fun js stuff in there too like !isNaN(parseInt()) to verify that the value we’re dealing with is a number and not a default option like “None”.
Most importantly I learned that we don’t need to use $(document).ready(function(){}); in Drupal modules. Instead we can use Drupal.behaviors.myModuleName = function (context) {} instead.

updateUpperField is the function I wrote to achieve this:
function updateUpperField(‘lowerFieldID’,’upperFieldID’,type:’int’|’date’)
Usage: call updateUpperField from within the document and include the two id’s of the fields in question as well as the type of field.