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 
March 24, 2012

jQuery Selector to Select Sections of a Table

Question by Amila

I have the following table in my page

<table>
<tr class="evenPack">
</tr>
<tr class="evenSubPack">
</tr>
<tr class="evenSubPack">
</tr>
<tr class="evenSubPack">
</tr>
<tr class="evenSubPack">
</tr>
<tr class="oddPack">
</tr>
<tr class="oddSubPack">
</tr>
<tr class="oddSubPack">
</tr>
<tr class="oddSubPack">
</tr>
<tr class="oddSubPack">
</tr>
<tr class="evenPack">
</tr>
<tr class="evenSubPack">
</tr>
<tr class="evenSubPack">
</tr>
<tr class="evenSubPack">
</tr>
<tr class="evenSubPack">
</tr>
</table>

The evenPack rows and oddPack rows are clickable. When the user clicks on one of them, I want the immediate SubPack rows to toggleSlide().

for example, if the user clicks on the first row, which is a evenPack. Then only the four evenSubPack rows that are immediately after the first evenPack should toggleSlide. It shouldn’t toggleSlide() the evenSubPack rows that are at the bottom of the table.

I can’t think of the selector to do this.

Answer by Starx

You can use nextUntil() for this:

$("tr").click(function() {
    var active =  $(this).attr("class").indexOf('even') > -1 ? 'odd' : 'even';
    //find if the clike in even, and find out out the next stop point will be
    $(this).nextUntil('.' + active + 'Pack').slideToggle();
});

Demo

February 26, 2012

Why can't I enable and uncheck an element with jQuery?

Question by just_name

I want to enable and set check = false by editing this code?

if (confirm('This widget will be removed, ok?')) {
    $(​'table tr input[type=hidden]').filter(function() {
        return $(this).val()​​​​​ == widgetId;
    }).siblings('input[type=checkbox]').attr({disabled: true, checked: true});

I try :

.siblings('input[type=checkbox]').removeAttr('disabled');

and this.

.siblings('input[type=checkbox]').attr({disabled:false, checked: false});

but in vain


EDIT 1:

<table><colgroup><col title="process name"></colgroup> <tbody><tr><td><input name="rlv_mainservices$ctrl0$hf_id" id="rlv_mainservices_ctrl0_hf_id" value="91" type="hidden"> <input id="rlv_mainservices_ctrl0_chb_sys" name="rlv_mainservices$ctrl0$chb_sys" type="checkbox"> <span id="rlv_mainservices_ctrl0_lbl_main_sys">pro1</span> </td></tr></tbody></table><table><colgroup><col title="processname"> </colgroup><tbody><tr><td><input name="rlv_mainservices$ctrl1$hf_id" id="rlv_mainservices_ctrl1_hf_id" value="92" type="hidden"><input id="rlv_mainservices_ctrl1_chb_sys" name="rlv_mainservices$ctrl1$chb_sys" type="checkbox"> <span id="rlv_mainservices_ctrl1_lbl_main_sys">pro2</span> </td></tr></tbody></table>

I wanna to enable input type=”checkbox” if the value of the input type =”hidden” that exist in the same <td> equal to the id of li(widget).This when the user click close.

Answer by Starx

Actually you can do this with jQuery.

To enable

$('#element').removeAttr('disabled');

To uncheck

$('#element').removeAttr('checked');

Or, even Combine them

$('#element').removeAttr('disabled').removeAttr('checked');

Just in case, you manipulated the attribute after it has been loaded, then it will no longer be available as an attribute from jQuery 1.6.

If you are using so and later, try .removeProp( propertyName ) [docs here] instead.

$('#element').removeProp('disabled').removeProp('checked');

Update

  • Update 1: It does work. Check a demo here.

  • Update 2: I have also simulated the manipulation and created another fiddle using .removeProp(), check it here

  • Update 3: Ok, check this update. It it compatible with your markup, however, you might have to use proper selector, for you. However, this will give you the entire idea.

...

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