Search code examples
jqueryradiobuttonlist

Selecting child input elements' CSS class


I'm using the following selector to set/remove disabled attributes on my input elements, upon button click:

var parentElement = $("#startingClass");
parentElement.find(":input").not(".dont-enable").not(".cascadeReset").removeAttr('disabled');

The problem I'm currently facing is that .NET renders RadioButtonLists in a terrible way:

<div id="startingClass">
    <table class="cascadeReset" id="tableID">
        <tbody>
            <tr>
                <td>
                    <input name="generatedID" disabled="disabled" id="generatedID" type="radio" value="12">
                    <label for="generatedID">Alternate</label>
                </td>
            </tr>
        </tbody>
    </table>
</div>

So, the jQuery selector is finding the input in the above example, and because the "cascadeReset" class is being applied to the table, instead of to the input, it is not ignoring it, and thus is removing the "disabled" tag.

Now, before anyone suggests - no, I can't add the CSS class "cascadeReset" to the input element. I've looked far and wide, and .NET doesn't seem to provide a way of doing it. The closest I could do, is wrap the input with a span tag that had the class.

Still, it seems to me as though I should be able to modify my jQuery selector to somehow still exclude the input.

Any ideas? The closest I could think of is to somehow tell the selector to look at the parent element, but A) I'm not sure how to write that, and B) it strikes me as being very easy to have that backfire and end up ignoring elements I'd rather it not.


Solution

  • It's probably horribly nonperformant (at least theoretically; on a small page it probably doesn't matter) but you can restrict inputs that are children of .cascadeReset like so:

    $("input").not(".cascadeReset input");
    

    To apply to your specific situation:

    var parentElement = $("#startingClass");
    parentElement.find(":input").not(".dont-enable").not(".cascadeReset input").removeAttr('disabled');