Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Passing ClientID to Javascript from asp.net textbox

Posted on 2007-10-02
Medium Priority
Last Modified: 2013-11-07
I have developed a site that uses both static ASP.net Textboxes and Dynamically created textboxes to hold dollar amounts.  I have also created a javascript that allows the user to type in " 1+2+3..." and onBlur will add the totals together.  Where i am running into an issue is I am not sure how to use the same function for all the textboxes.  I understand that the client ID will tell the javascript where to look, but how to I pass that client ID from the asp.net side in the onBlur? I know I will have to pass an argument un the javascript function, but have had trouble figuring this out. Any help is appreciated.

function solve() {
         with (Math) {
              answer = eval(document.getElementById("<%=txtCandy.ClientID%>").value);
              document.getElementById("<%=txtCandy.ClientID%>").value = answer;

ASP textbox:
          Width = "90px"
          ToolTip="Candy Total for the Day"

Attributes added in the Page_load section:
        txtCandy.Attributes.Add("onBlur", "solve(this);return false;")
Question by:JaCrews
  • 2
LVL 19

Accepted Solution

dakyd earned 2000 total points
ID: 20001949
Well, it looks like you're already passing "this" to the javascript function, so you already have what you need.  The "this" keyword will pass a reference to the object from which the function was called, so in a sense, your javascript function is already dynamic.  Give this a shot:

function solve(obj) {
         with (Math) {
              answer = eval(obj.value);
              obj.value = answer;

Attributes added in the Page_load section:
        txtCandy.Attributes.Add("onBlur", "solve(this);return false;")

You'd just add the same onBlur for every other textbox that you want to exhibit the same behavior.  Hope that helps.

Author Comment

ID: 20002070
I am so angry right now! thank you so much, it's always the little things that get me.
LVL 19

Expert Comment

ID: 20002213
Heh, we've all done it.  At least you got it working now.  Cheers.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses

572 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