(function ($) { $.widget("portal.userproperties", { options: { parentpageref: null }, _create: function () { // bind relevant events this.bind(); }, bind: function() { // reference this plugin var $that = this; // set up the correct state view $that.displaystatefield($that.options.parentpageref.find(".country_selection")); // hook into the country dropdown $that.options.parentpageref.on("change", ".country_selection", function() { $that.displaystatefield(this); }); // hook into the submit button for the form this is embedded in $that.options.parentpageref.closest("form").submit(function () { var $form = $(this); $.post("", $form.serialize()); }); }, displaystatefield: function (countryfield) { var $countryfield = $(countryfield); var country = $countryfield.children("option").filter(":selected").text() var $stateitem = $countryfield.closest("form").find(".state_item"); var $stateddl = $stateitem.find("select.input_select"); var $statetext = $stateitem.find("input.input_text"); if (country == "" || country == "United States") { $statetext.val(""); SwitchElementViews($stateddl, $statetext); } else { $stateddl.val(""); SwitchElementViews($statetext, $stateddl); } }, destroy: function () { this.element.removeData(); } }); })(jQuery);