Skip to content

Instantly share code, notes, and snippets.

@vfontjr
Last active Jan 7, 2022
Embed
What would you like to do?
<script>
jQuery(document).ready(function($) {
"use strict";
// populate primary contact name and email from registration credentials
$("#field_devdir_copy_name_and_email").on("change", function() {
if ( $(this).prop('checked') ) {
bindCurrent();
} else {
unbindCurrent();
}
});
var bindCurrent = function() {
// First copy values
$("#field_devdir_primary_contact_first").val($("#field_devdir_first_name").val()).attr("readonly",true); //first name
$("#field_devdir_primary_contact_last").val($("#field_devdir_last_name").val()).attr("readonly",true); // last name
$("#field_devdir_primary_contact_email").val($("#field_devdir_email").val()).attr("readonly",true); // email
/* Then bind fields */
/* first name */
$("#field_devdir_first_name").keyup(function() {
$("#field_devdir_primary_contact_first").val($(this).val());
});
/* last name */
$("#field_devdir_last_name").keyup(function() {
$("#field_devdir_primary_contact_last").val($(this).val());
});
/* email */
$("#field_devdir_email").keyup(function() {
$("#field_devdir_primary_contact_email").val($(this).val());
});
};
var unbindCurrent = function() {
/* unbind fields */
$("#field_devdir_first_name").unbind("keyup");
$("#field_devdir_last_name").unbind("keyup");
$("#field_devdir_email").unbind("keyup");
/* clear readonly and disabled attributes */
$("#field_devdir_primary_contact_first").removeAttr("readonly");
$("#field_devdir_primary_contact_last").removeAttr("readonly");
$("#field_devdir_primary_contact_email").removeAttr("readonly");
/* clear fields of copied values */
$("#field_devdir_primary_contact_first").val("");
$("#field_devdir_primary_contact_last").val("");
$("#field_devdir_primary_contact_email").val("");
/* set focus to primary contact first name */
$("#field_devdir_primary_contact_first").focus();
};
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment