Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

show and hide elements based on data-* attribute

This seems like it should be trivial for JQuery to do, but this function is hiding the whole form... can someone point me in the right direction?

$('form')
        .children()
        .filter(function(){
            return $(this).data('show', 'pro')
        })
        .show();
$('form')
         .children()
         .filter(function(){
             return $(this).data('show', 'home')
         })
         .hide();
like image 663
Chris Sobolewski Avatar asked Dec 04 '22 18:12

Chris Sobolewski


1 Answers

You're passing 2 arguments to the data method, thereby setting it instead of retrieving the old value.

Use this instead:

$('form')
        .children()
        .filter(function(){
            return $(this).data('show') === 'pro';
        })
        .show();
$('form')
         .children()
         .filter(function(){
             return $(this).data('show') === 'home';
         })
         .hide();

You could also cache your selector for performance (or use end).

like image 63
Joseph Silber Avatar answered Dec 31 '22 04:12

Joseph Silber