That code only sets the visibility of the #state and #province elements when
the document is first loaded. It's missing the other part of the
functionality from the original code and from Ricardo's version and mine:
Updating that visibility whenever the #edit-profile-country element changes.

That's the purpose of the .trigger() in Ricardo's code, and the separate
function called both at load time and on the change event in my code.

-Mike

> From: johan.bores...@gmail.com
> 
> Good abstraction Ricardo. But I wouldn't use trigger like 
> that. It feels a bit overkill plus that it might hurt 
> performance on the client.
> 
> My approach would be something like this, doing exactly the 
> same + better performance:
> 
> $.fn.showIf = function( show, arg ) {
>    return this[ show ? 'show' : 'hide' ]( arg ); };
> 
> $(document).ready(function(){
>     $('#edit-profile-country').each(function(){
>        var value = $(this).val();
>        $('#state').showIf( value == 'US' );
>        $('#province').showIf( value == 'CA' );
>     })
> });
> 
> 
> / Johan
> 
> 
> 
> On 4 Apr, 22:46, Ricardo <ricardob...@gmail.com> wrote:
> > Nice. I'd just take advantage of event triggering to make it a bit
> > simpler:
> >
> > $.fn.showIf = function( show, arg ) {
> >    return this[ show ? 'show' : 'hide' ]( arg );
> >
> > };
> >
> > $().ready(function(){
> >
> >     $('#edit-profile-country').change(function(){
> >        var value = $(this).val();
> >        $('#state').showIf( value == 'US' );
> >        $('#province').showIf( value == 'CA' );
> >     }).trigger('change');
> >
> > });
> >
> > cheers,
> > - ricardo
> >
> 

Reply via email to