For those lurking, Michael Geary, over in the jQuery (English) Forum, posted a killer alternative way of looking at this one.
http://is.gd/r3bX (Is there a way to link in-thread to Google group? I could not find it) Quoting from that link: Michael Geary: I would code it like this: $.fn.showIf = function( show, arg ) { return this[ show ? 'show' : 'hide' ]( arg ); }; $(function() { var $epc = $('#edit-profile-country'); function setVisibility() { var value = $epc.val(); $('#state').showIf( value == 'US' ); $('#province').showIf( value == 'CA' ); } setVisibility(); $epc.change( setVisibility ); }); The one-line .showIf() plugin makes it easy to either show or hide an element depending on a condition. .... **--** Steve On Apr 6, 11:24 am, Scott González <[email protected]> wrote: > $(document).ready(function() { > var $profile = $('#edit-profile-country'), > $state = $('#state'), > $province = $('#province'); > > function toggleStateProvince() { > var val = $profile.val(); > > $province.hide(); > $state.hide(); > > if (val == 'US') { > $state.show(); > } else if (val == 'CA') { > $province.show(); > } > } > toggleStateProvince(); > $profile.change(toggleStateProvince); > > }); > > As for checking for the existence of an element, there's two things to > keep in mind: > 1) You can check the length of the jQuery object to see if it found > any elements: if ($('#state').length) { ... } > 2) If you don't care what happens if the element doesn't exist, > there's no need to check for it: $('#state').show() just won't do > anything if #state doesn't exist (no errors). --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "jQuery UI" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/jquery-ui?hl=en -~----------~----~----~----~------~----~------~--~---
