1

I'm new with jQuery and fairly new to JS (a little knowledge) and I'm wanting to create a jQuery code.

Firstly, here is my HTML code:

<div id="user-controls">
    <div class="choice" id="choice-all" onclick="showAll();">All</div>
    <div class="choice" id="choice-asus" onclick="justASUS();">ASUS</div>
    <div class="choice" id="choice-htc" onclick="justHTC();">HTC</div>
</div>

<div id="devices">
    <div class="android asus">Nexus 7</div>
    <div class="android htc">One S</div>
    <div class="android htc">One X+</div>
    <div class="android asus">Transformer Prime</div>
    <div class="winph htc">Windows Phone 8X</div>
</div>

I'm wanting a jQuery code that would do the following:

  1. If I click on the #choice-asus DIV, then all DIVs with the class .htc would be set to display="none"

  2. If I click on the #choice-htc DIV, then all DIVs with the class .asus would be set to display="none"

  3. If I click on the #choice-all DIV, then all DIVs would be set to display="inline-block" (this is also the default setting when the page first loads)

I've already tried the following code, but it doesn't do anything.

$(document).ready(function(){
    $("#choice-htc").click(function(){
        $(".htc").hide();
    })
});

Thank you for any help, Dylan.

DylRicho
  • 895
  • 1
  • 10
  • 25
  • Where is the element with "HTC" id (it is case sensitive) – AnandVeeramani Oct 25 '12 at 06:18
  • Oops. Sorry. That was taken from a separate test file but it would have done the same as #choice-htc in this code. – DylRicho Oct 25 '12 at 06:37
  • @AnandVeeramani I've fixed it now. I used HTC to see if it would hide its own matching DIV elements but it didn't work. If it did, I would have swapped the coding afterwards. – DylRicho Oct 25 '12 at 08:43

6 Answers6

2

So many choices :) http://jsfiddle.net/9RtUE/

    $(function(){
        $("#user-controls").on('click','div',function(){
           var classToShow = this.id.split('-')[1],
               filter = classToShow === "all" ? 'div': '.' + classToShow;
           $("#devices").children().show().not(filter).hide();
         }); 
     });
brains911
  • 1,310
  • 7
  • 5
  • Hey. :) Thank you for the quick response. The code above works perfectly well with jsFiddle but it fails to work on my site. I've got the latest version of jQuery linked in via an external file and all error consoles are blank. I'm not sure what's going on. http://www.dylricho.com/archive/learning/Dropdown_Styles/new4.php – DylRicho Oct 25 '12 at 08:40
  • It looks like you're having some jquery issues. '$' is undefined on your page. Perhaps you are having some sort of conflict with other included javascript. – brains911 Oct 25 '12 at 17:29
  • I've found it out to be this code - http://www.dylricho.com/s/FluidTextResizer.js – DylRicho Oct 25 '12 at 23:54
  • But it doesn't conflict if I comment out the `jQuery.noConflict()` line, and the text resizer still works after, along with your code. – DylRicho Oct 26 '12 at 00:27
  • see [this link](http://stackoverflow.com/questions/10371539/why-define-anonymous-function-and-pass-it-jquery-as-the-argument) for info/suggestions on what you can do. Hope it helps. – brains911 Oct 26 '12 at 01:01
1

try using jquery

Demo

$('#choice-all').click(function(){
  $('.htc, .asus').show();
});

$('#choice-asus').click(function(){
  $('.asus').show();
  $('.htc').hide();
});

$('#choice-htc').click(function(){
  $('.htc').show();
  $('.asus').hide();
});   
Pragnesh Chauhan
  • 8,363
  • 9
  • 42
  • 53
  • Hello, your code works perfectly on jsFiddle but it fails to work on my site. I have the latest jQuery library linked in from an external file so I'm not sure what the problem is. Could you help? http://www.dylricho.com/archive/learning/Dropdown_Styles/new.php – DylRicho Oct 25 '12 at 08:22
  • It's okay. It appeared to be a conflict issue between another script. Using '$' didn't work but using 'jQuery' did. :) – DylRicho Oct 27 '12 at 09:49
  • @DylRicho , ok for that you can also use `jQuery.noConflict();` – Pragnesh Chauhan Oct 27 '12 at 09:51
1

Demo here

$(document).ready(function(){   
    $(".choice").click(function(){
         $(".android,.winph").hide();
        if($(this).attr("id")=="choice-all"){
            $(".android,.winph").show();
        }else if($(this).attr("id")=="choice-asus"){
            $(".asus").show();
        }else if($(this).attr("id")=="choice-htc"){
            $(".htc").show();
        }
    });
});
Anupam
  • 7,966
  • 3
  • 41
  • 63
1

to keep it easy and clean you should use a solution such as this one

$(function(){
    $('#choice-asus').on('click', function(){
        $('#devices > div:not(.asus)').hide();
    });
});

it basically says, if you click on #choice-asus, hide all divs in #devices which have no class asus.

you can extend / modify this for your own needs. besides, its recommend to use jquerys .on() method instead click/bind or what ever handler you'd apply.

Stefan
  • 718
  • 9
  • 13
  • Hey Stefan. Your code appears to work fine at jsFiddle, but fails to work on my site. I've got the latest version of jQuery linked in via an external script and nothing is shown in Chrome's error console. Do you have any suggestions? http://dylricho.com/archive/learning/Dropdown_Styles/new3.php – DylRicho Oct 25 '12 at 08:37
  • i guess i found it! it seems like $ is undefined, just change the $ in my snippet to jQuery and it should work – Stefan Oct 25 '12 at 09:13
  • I've adopted a new way through trial and error. What do you think? :) http://dylricho.com/archive/learning/Dropdown_Styles/new-custom.php – DylRicho Oct 25 '12 at 11:11
  • it doesn't really matter if you use css or the hide() method since the hide() method will change the css as well, either way your problem seems to be solved! – Stefan Oct 25 '12 at 17:33
  • The only difference is that the css() method seems to work. Thank you for the help buddy. :) – DylRicho Oct 25 '12 at 23:28
  • Ah I think I know what I did. I only inserted the code once, thinking it'd do them all. Then I re-read your description and I've inserted it 3 times for each one. The show() and hide() method works fine now. Thank you for the help buddy. :) – DylRicho Oct 25 '12 at 23:34
1
$(document).ready(function(){
    if($('div').attr('class')=='X')
    {
         $('div').not($(this)).css('display','none');
    }
});
Hiren H Patel
  • 167
  • 3
  • 12
1

You can try the following code-

function showAll(){
    $("#devices div").show();
}
function justASUS(){
    $("#devices div").hide();
    $("#devices .asus").show();
}
function justHTC(){
    $("#devices div").hide();
    $("#devices .htc").show();    
}

demo here.

Anup Das Gupta
  • 772
  • 1
  • 7
  • 24
  • Hi. I appreciate your response. The code appears to work fine in jsFiddle, but doesn't work on my site. I have the latest version of jQuery linked in via an external script and Google Chrome's error console is blank. Can you help? http://dylricho.com/archive/learning/Dropdown_Styles/new2.php – DylRicho Oct 25 '12 at 08:35
  • Which browser its not working. I have checked in chrome and working fine. – Anup Das Gupta Oct 26 '12 at 04:57
  • It's okay. It appeared to be a conflict issue between another script. Using '$' didn't work but using 'jQuery' did. – DylRicho Oct 27 '12 at 09:48