Solved

How do I reference an asp.net label id in a javascript function call from asp.net without knowing the generated id for that label

Posted on 2013-01-07
4
582 Views
Last Modified: 2013-11-19
A have a textarea (txtSMSMelding), a JavaScript function (taCount) counting the number of characters in the textarea and a label (lblAntTegn) containing the counter.

The counting is done as I am typing in the characters.
The problem is that I have to know the exact ID of the the counter generated for the browser.

In the code example below I am transfering the label with the ID "lblAntTegn" to the JavaScript function "taCount". But because the label is inside a usercontrol named ucSMS, witch in turn is inside another usercontrol  named ucMerInfoForetak, I have to type in the object name 'ucMerInfoForetak_ucSMS_lblAntTegn'.

This means that if I am moving this code somewhere I have to change the object name passed to the JavaScript function.
How can I specify this object name more generally in the code below?

                          <textarea id="txtSMSMelding" runat="server" onkeypress="return taLimit(this);" onkeyup="return taCount(this,'ucMerInfoForetak_ucSMS_lblAntTegn');"
                                        name="Description" rows="10" cols="50">

                                    </textarea>
0
Comment
Question by:Joar
  • 2
4 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 38750177
add the following JS snippet:

<script type="text/javascript">

  var lblAntTegnID = '<%= lblAntTegn.ClientID %>';

</script>

Open in new window


now u can reference your label via lblAntTegnID variable.
0
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 38750195
<textarea id="txtSMSMelding" runat="server" onkeypress="return taLimit(this);" onkeyup="return taCount(this,'<%= lblAntTegn.ClientID %>');"
                                        name="Description" rows="10" cols="50">

</textarea>
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 38750200
ultimately, you'll have something like:

<textarea id="txtSMSMelding" runat="server" onkeypress="return taLimit(this);" onkeyup="return taCount(this, '<%= lblAntTegn.ClientID %>');"
                                        name="Description" rows="10" cols="50">

                                    </textarea>

Open in new window

0
 

Author Closing Comment

by:Joar
ID: 38750209
That simple!
I feel like a fool when moving into the JavaScript world...
Did function!
Thanks!!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will learn how to dynamically set the form action using jQuery.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now