Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to access the ClientID from a ServerControl in an external JavaScript
    text
    copied!<p>At the moment, if I use JavaScript in my SharePoint projects, I add the code into the *.ascx file, in a <code>&lt;script type="text/javascript"&gt;&lt;/script&gt;</code> block and create for each element a variable for the <code>ClientID</code>.</p> <p>For example:</p> <pre><code>var test = '&lt;%= TextBox1.ClientID %&gt;'; </code></pre> <p>Now I would like to add an external JavaScript to my projects and insert the code there. But how could I access to the <code>ClientID</code>? In the external JavaScript I can’t use <code>&lt;%= TextBox1.ClientID %&gt;</code>. I found this: <a href="https://stackoverflow.com/questions/911042/ajax-asp-net-referencing-server-controls-in-external-file">referencing server controls in external file</a> but I doesn’t understand, how this should work. It would be awesome, if someone could explain my, how to access the ids.</p> <p>By the way, why this:</p> <pre><code>&lt;script type="text/javascript"&gt; var ClientIDs = { test1 : '&lt;%= TextBox1.ClientID %&gt;', test2 : '&lt;%= TextBox2.ClientID %&gt;' } function SetButtonStatus() { alert($(ClientIDs.test1).value); } &lt;/script&gt; </code></pre> <p>doesn’t work, no message would be shown?</p> <p>Greetz</p> <p><strong>Edit 1:</strong></p> <p>Okay, I could just use textBox1 in my external script? I did it this way, this is in my *.ascx file:</p> <pre><code>&lt;script type="text/javascript"&gt; var ClientIDs = { textBox1: '&lt;%= textBox1.ClientID %&gt;', textBox2: '&lt;%= textBox2.ClientID %&gt;' } &lt;/script&gt; </code></pre> <p>In my external script I have just a function to test it:</p> <pre><code>function test () { alert($(ClientIDs.textBox1).val(); } </code></pre> <p>I also tested it with <code>"#" +</code>. Every time test() is executed, I get following error:</p> <pre><code>"document.getElementById(...)" is null or not an object </code></pre> <p><strong>Edit 2:</strong> I missed a <code>)</code> in the alert. But now I get a message that the variable is not defined. If I use: <code>$('#' + ClientIDs.SumbitSearch).val()</code> I just get the Text and not the ID of my control.</p> <p><strong>Edit 3:</strong> At the moment I use:</p> <pre><code>&lt;script type="text/javascript"&gt; var ClientIDs = { test1 : '&lt;%= TextBox1.ClientID %&gt;', test2 : '&lt;%= TextBox2.ClientID %&gt;' } function test() { alert($('#' + ClientIDs.test1).attr("id"))); } &lt;/script&gt; </code></pre> <p>In my *.ascx file, it works. I don't like that way... It doesn't work in a external JS, the references doesn't work. If someone have some other ideas, which would work with .net 3.5 it would be nice, if he let me know.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload