简体   繁体   中英

find control id by jquery and pass it to function to toggle it

find control id by jquery and pass it to function to toggle it.

 function toggleDiv() { var $usr = $this.find('[id*=shoow]'); $($usr).toggle('slow'); } 
 <asp:LinkButton runat="server" OnClientClick="toggleDiv(); return false;">Detials</asp:LinkButton> <table> <tr> <td id="shoow" style="width:650px;height:100px;background-color:blue; display:none;"> </td> </tr> </table> 

Initially, since you have declared a server side control, LinkButton , you have to provide an ID , for it.

<asp:LinkButton  ID="linkButtonId" runat="server">Details</asp:LinkButton>

You don't have to use the find function for this purpose. Actually, we use this function for the following reason:

Get the descendants of each element in the current set of matched elements, filtered by a selector, jQuery object, or element.

What you could do?

I suggest at the bottom of your page before the body's closing tag to add this script

<script type="text/javascript">
    $(function(){
        $("#"+<%=linkButtonId.ClientID%>).click(function(){
            $('#shoow').toggle('slow');
        });
    })
</script>

Important Note

You should load the jQuery before this script.

i want to give id from code behind by find control function or jquery funtion becuase i have datalist generat id's random.

You could select then the element like below:

$("[id*="+"shoow"+"]").toggle('slow');

Try the following snippet to see what I mean:

 $(function(){ $("#buttonId").click(function(){ $("[id*="+"shoow"+"]").toggle('slow'); }) }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="buttonId">Details</button> <table> <tr> <td id="3shoow2" style="width:650px;height:100px;background-color:blue; display:none;"> </td> </tr> </table> 

I agree the answer of Christos.

Yes you don't have to use find function

Instead please use jQuery:

$('#shoow').toggle('slow');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM