May 12, 2012

jquery selector multiple classes on set of <div>'s (contains one class, not the other)

Question by Dave Jay

This may be easier than what I’ve tried. I have a set of <div>‘s such as <div id="site-abc" .. "site-def"> and each has similar HTML content. The DIV’s have multiple classes like "ui-tabs-panel class2 ui-tabs-hide". One does not have ui-tabs-hide. That’s what I need a selector for, and then from that I need to find unique text within that DIV that’s within bingo!.

What’s the right jQuery selector code for the following steps: A) find right <div> with class="ui-tabs-panel" but NOT CONTAINING "ui-tabs-hide"; B) take that selector result and find ('th.target2') and get the .text()?

var tabs = $('#editors.not(.ui-tabs-hide'); 
var my_text = tabs.find('th.target2').text();

<div class="grid-editor">
    <div id="editors" class="ui-tabs">
        <div id="site-abc" class="ui-tabs-panel class2 ui-tabs-hide"> 
            //duplicates HTML like "site-ghi" including <th>bingo!</th>
        </div>
        <div id="site-def" class="ui-tabs-panel class2 ui-tabs-hide">
            //duplicates HTML like "site-ghi" including <th>bingo!</th>
        </div>
        <div id="site-ghi" class="ui-tabs-panel class2”>
            <table class=”my-table”>
                <thead>
                    <tr>

                        <th class="target1">abc1</th>
                        <th class="target2">bingo</th>
                    </tr>
                </thead>
        </div>
    </div>
</div>

Answer by Starx

You almost got it. Use :not selector to omit the one you don’t need.

var divs = $('.ui-tabs-panel:not(.ui-tabs-hide)'); 

I am not sure if this is related to the problem, but there is mistake in your selector statement as well.

var tabs = $('#editors.not(.ui-tabs-hide');
                                      //^ No Closing bracket 

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!