Search code examples
jqueryasp.netgridviewtelerikjquery-selectors

find and toggle div in dom via jquery (asp.net gridview, radcombobox)


i use a radcombobox within a asp.net gridview. Now i want, if the user select a special value from the combobox, to toogle a divs visibility. But because of the rendered code i have no idea what i have to do.

This is the rendered code:

<td>
    <div class="colColor DivInHere" style="margin-top:8px;margin-right:2px;display:inline-block;float:left;">Artikel</div>
    <div class="colColor" style="margin-top:7px;float:left;margin-left:28px;">
        <div id="ctl00_ContentPlaceHolder1_gvRechnungsposition_ctl02_rcbArtikel" class="RadComboBox RadComboBox_Metro" style="width:200px;margin-left:5px;">
            <table class="rcbFocused" style="border-width: 0px; border-collapse: collapse;" summary="combobox">
                <tbody>
                    <tr>
                        <td class="rcbInputCell rcbInputCellLeft" style="width:100%;">
                            <input id="ctl00_ContentPlaceHolder1_gvRechnungsposition_ctl02_rcbArtikel_Input" class="rcbInput radPreventDecorate" type="text" style="color:rgb(166, 204, 213);" value="Bitte wählen ..." name="ctl00$ContentPlaceHolder1$gvRechnungsposition$ctl02$rcbArtikel" autocomplete="off">
                        </td>
                        <td class="rcbArrowCell rcbArrowCellRight">
                            <a id="ctl00_ContentPlaceHolder1_gvRechnungsposition_ctl02_rcbArtikel_Arrow" style="overflow: hidden;display: block;position: relative;outline: none;">select</a>
                        </td>                   
                    </tr>
                </tbody>
            </table>
            <input id="ctl00_ContentPlaceHolder1_gvRechnungsposition_ctl02_rcbArtikel_ClientState" type="hidden" name="ctl00_ContentPlaceHolder1_gvRechnungsposition_ctl02_rcbArtikel_ClientState" autocomplete="off" value="{"logEntries":[],"value":"Benutzerdefiniert","text":"Benutzerdefiniert","enabled":true,"checkedIndices":[],"checkedItemsTextOverflows":false}">
        </div>
    </div>
    <div style="clear:both"></div>
</td>
<td>
    <div class="colColor in" style="margin-left:10px;margin-right:10px;display:none;">
</td>

This is my code before rendered:

...    
<asp:TemplateField>
                        <ItemTemplate>
                            <div class="colColor DivInHere" style="margin-top:8px;margin-right:2px;display:inline-block;float:left;">Artikel</div>
                            <div class="colColor" style="margin-top:7px;float:left;margin-left:28px;">
                                <telerik:RadComboBox runat="server" ID="rcbArtikel" Width="200px" Skin="Metro" AllowCustomText="true"
                                    EmptyMessage="Bitte wählen ..." CloseDropDownOnBlur="true" LoadingMessage="... Ladevorgang ..."
                                    Filter="Contains" MarkFirstMatch="true" ForeColor="#a6ccd5" style="margin-left:5px;"
                                    DataTextField="ArtikelBeschreibung" DataValueField="ArtikelID" OnClientSelectedIndexChanged="OnClientSelectedIndexChanged">
                                </telerik:RadComboBox>
                            </div>
                            <div style="clear:both"></div>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="ArtikelID" Visible="false"></asp:BoundField>
                    <asp:TemplateField>
                       <ItemTemplate>
                           <div class="colColor in" style="margin-left:10px;margin-right:10px;display:none;">
                               Beschreibung <asp:TextBox ID="tbBenutzerdefiniert" runat="server" MaxLength="250" Text="0" Width="200px" CssClass="inputWhite"></asp:TextBox>
                           </div>
                       </ItemTemplate>
                    </asp:TemplateField>
...

I want, if rcbArtikel fires OnClientSelectedIndexChanged to toggle this div: <div class="colColor in" style="margin-left:10px;margin-right:10px;display:none;"> by its class .in

This is my function:

            function OnClientSelectedIndexChanged(sender, eventArgs) {
            var item = eventArgs.get_item();

            var selItem = item.get_text();
            if (selItem == "Benutzerdefiniert") {
                //$(".in").toggle();
                $(this).closest('td > tr > tbody > table > div > div > td').next('td').find('.in').show();
            };

        }

I hope you understand what i'm trying to do and thanks for reading


Solution

  • Okay, i found a solution that works for me. Maybe it helps someone with an similar problem:

    The main problem was, that i forgot to get the right ID from my radcombobox. I did it like this: var currentId = '#' + $(sender).attr('_clientStateFieldID');

    This is now my function:

    function OnClientSelectedIndexChanged(sender, eventArgs) {
    
                var item = eventArgs.get_item();
                var selItem = item.get_text();
                var currentId = '#' + $(sender).attr('_clientStateFieldID');
    
                if (selItem == "Benutzerdefiniert") {
                    $(currentId).parent().parent().parent().next('td').find('div.in').toggle();
                } else {
                    $(currentId).parent().parent().parent().next('td').find('div.in').hide();
                };
    
            }
    

    Again, there are certainly better solutions out there!