Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6731739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:33:53+00:00 2026-05-26T10:33:53+00:00

At the moment, if I use JavaScript in my SharePoint projects, I add the

  • 0

At the moment, if I use JavaScript in my SharePoint projects, I add the code into the *.ascx file, in a <script type="text/javascript"></script> block and create for each element a variable for the ClientID.

For example:

var test = '<%= TextBox1.ClientID %>';

Now I would like to add an external JavaScript to my projects and insert the code there.
But how could I access to the ClientID? In the external JavaScript I can’t use <%= TextBox1.ClientID %>. I found this: referencing server controls in external file but I doesn’t understand, how this should work. It would be awesome, if someone could explain my, how to access the ids.

By the way, why this:

<script type="text/javascript">
    var ClientIDs = {
        test1   : '<%= TextBox1.ClientID %>',
        test2   : '<%= TextBox2.ClientID %>'
        }

    function SetButtonStatus() {    
            alert($(ClientIDs.test1).value);
        }
</script>

doesn’t work, no message would be shown?

Greetz

Edit 1:

Okay, I could just use textBox1 in my external script?
I did it this way, this is in my *.ascx file:

<script type="text/javascript">
    var ClientIDs = {
        textBox1:    '<%= textBox1.ClientID %>',
        textBox2:    '<%= textBox2.ClientID %>'
    }
</script>

In my external script I have just a function to test it:

function test () {
alert($(ClientIDs.textBox1).val();
}

I also tested it with "#" +. Every time test() is executed, I get following error:

"document.getElementById(...)" is null or not an object

Edit 2:
I missed a ) in the alert. But now I get a message that the variable is not defined.
If I use: $('#' + ClientIDs.SumbitSearch).val() I just get the Text and not the ID of my control.

Edit 3:
At the moment I use:

<script type="text/javascript">
    var ClientIDs = {
        test1 :    '<%= TextBox1.ClientID %>',
        test2 :    '<%= TextBox2.ClientID %>'
    }

    function test() {
       alert($('#' + ClientIDs.test1).attr("id")));
    }
</script>

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:33:54+00:00Added an answer on May 26, 2026 at 10:33 am

    To explain, and simplify the question that you’re linking to, all they are doing is setting a JavaScript variable from the page/server control, and reading that variable from an external JavaScript file.

    For example, your *.ascx file will contain this JavaScript:

    var textBox1 = '<%= TextBox1.ClientID %>';
    

    Then, your external JavaScript file can just reference the variable textBox1.

    Now, there are other ways to accomplish this. If you’re using ASP.NET 4, you can use a new property ClientIDMode to prevent ASP.NET from changing your IDs. If you’re not using ASP.NET 4, could also simply add a CSS class to the elements you want to select, and just change your jQuery selector to use a class (slightly slower than using an ID though).

    Lastly, you’ll need to use the # when evaluating a jQuery selector for an element id, so this will work:

    alert($('#' + ClientIDs.test1).val());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an article in which I use PHP code inside the text editor
I am trying to use the following javascript code to alert the user when
I would like to use JavaScript to do the following: Build an XML file
At the moment we use HSQLDB as an embedded database, but we search for
At the moment I use PHP for almost everything I develop for the Web
So right now I'm bashing my head - at the moment we use an
This question is for C# 2.0 Winform. For the moment I use checkboxes to
I have two nested loop in XSL like this, at this moment I use
I use a stream reader to import some data and at the moment I
I need to use sendmail from Macs in an office. At the moment, I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.