Passing ClientID to Javascript from textbox

Posted on 2007-10-02
Last Modified: 2013-11-07
I have developed a site that uses both static 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 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
    LVL 19

    Accepted Solution

    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

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

    Expert Comment

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

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    Suggested Solutions

    IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

    754 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

    17 Experts available now in Live!

    Get 1:1 Help Now