Created
September 6, 2011 11:11
-
-
Save calvincorreli/1197288 to your computer and use it in GitHub Desktop.
Link two input fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example usage: | |
// $('#entrant_1_email').linkTo('#purchase_email'); | |
// This will cause any changes to #purchase_email to be reflected in #entrant_1_email | |
// unless #entrant_1_email has been changed to be something different from #purchase_email | |
(function($){ | |
$.fn.linkTo = function(link_to, options) { | |
return this.each(function() { | |
var $link_to = $(link_to); | |
var changed_manually = false; | |
var $this = $(this); | |
if ($link_to.val() != $this.val()) { | |
changed_manually = true; | |
} | |
$link_to.change(function(){ | |
if (!changed_manually) { | |
$this.val($link_to.val()); | |
} | |
}); | |
$this.change(function(){ | |
if ($this.val() == "") { | |
changed_manually = false; | |
} else if ($this.val() != $link_to.val()) { | |
changed_manually = true; | |
} | |
}); | |
}); | |
} | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment