Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript how to make this better function and less code.

  • This topic is empty.
Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #35167
    Attila Hajzer
    Participant

    http://jsfiddle.net/attilahajzer/dRqjK/

    how do i make this function better without looking so jittery and glitchy and with possibly less code… (without the use of a plugin or an external function.

    #90662
    timmey
    Member

    for less code maybe something like this:


    var value = $(this).val();
    if( value == 'all') {//check which radio button is clicked
    $(".box").fadeIn('slow');
    } else if(value == 'red') {
    $(".red").fadeIn('slow').siblings('.green, .yellow, .blue').hide();
    } else if(value == 'green') {
    $(".green").fadeIn('slow').siblings('.red, .yellow, .blue').hide();
    }else if(value == 'blue') {
    $(".blue").fadeIn('slow').siblings('.green, .yellow, .red').hide();
    }else if(value == 'yellow') {
    $(".yellow").fadeIn('slow').siblings('.green, .red, .blue').hide();
    }

    im no expert though.. :)

    #90665
    Mottie
    Member

    Try this (updated demo)

    $(document).ready(function() {
    // attack a click event on all radio buttons with name 'category'
    $("input[name=category]").click(function() {
    var sel = $(this).val();
    $('.' + sel).fadeIn('slow');
    if (sel !== 'box') { $('.box:not(.' + sel + ')').fadeOut('fast'); }
    });
    });

    I changed the radio selection value for all from “all” to “box” to match the class name of all the boxes – it just made coding easier ;)

    Oh and I removed the “:radio” from the input selector… I think it’s going away in a future version of jQuery, so might as well get rid of it now.

    #90707
    Mottie
    Member

    *shrug* I’m the red-headed step-child with a side of chopped liver!

    #90904
    Mottie
    Member

    Wow, it feels weird being completely ignored… I had it working with the fadeOut eight posts ago… but hey, I guess I’m glad you got it working… and you’re welcome.

    #90931
    TheDoc
    Member

    Mottie this has been hilarious to follow.

    #90943
    standuncan
    Member

    +1

    #90944
    Mottie
    Member

    LOL thanks guys… wait, how am I hilarious? o.O

    #90950
    standuncan
    Member

    More-so it’s the situation I’d say.

Viewing 9 posts - 1 through 9 (of 9 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.