Search code examples
javascriptjqueryasp.netuser-controlsascx

Find control where javascript is running


I have a user control named uscUserControl.ascx and a page named test.aspx. I have used user control twice on my page. My user control looks like.

<div id="div1">
</div>
<script>
   $(document).ready() (function() {$('[id$=div1]').html('Control called');}  );
</script>

But it only modifies first div and second is empty. So how to make script modify the element nearest to it?


Solution

  • We can solve it using combination of ClientID and partial Id as below

    $(document).ready() (function() {$('[id*=<%= ClientID %>][id$=div1]').html('Control called');}  );