I have a very novice question, so apologies if the answer to this is obvious.

I am using JQuery to toggle the contents of items based on whether the item has been clicked. I have been able to successfully implement the toggle feature.

I now need to have it load with the first two items set to show() with the rest set to hide(). I have given a unique class name to these first 2 items. I know that I can simply do a $('div.activeitem').show() and then hide thee rest, but I'd prefer to setup a condition.

I am a JQuery novice, so I don't know how to target these elements or their classes in a conditional statement. I've searched google but have been unsuccessful. I want a conditional that asks if the div "newsinfo" also has the class "jopen" then show(), else hide().

Thanks for your help. I have attached my code to help you understand the context of my question:

<script type="text/javascript">
                    // Here is where I'd like to implement a conditional
                    $('div.newsinfo').hide(); // this would be part of my else
                    $('h5.newstoggle').click(function() {
                        return false;
Was it helpful?


How about simply



there is hasClass() function. Better way is using toggleClass().

For example:


after first click class will be added, after second - removed... and so on ^^

JQuery has an .hasClass function.


    //remove selection
    //remove the selected class from the currently selected one
    $(".selectableItem .selected").removeClass("selected");
    //add it to this one

Why don't you add a default css to jopen class to display: block and the others to display: none ?

something like

 .newsinfo {display: none}
 .jopen {display:block!important}

Just use selectors. For example, if all divs with the class "newsinfo" are visible by default:


If they're all hidden by default:


Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow