Why is passing the ClientID to a javascript function passing the entire control?

If you pass the ClientID to a javascript function as a parameter without including single quotes around it, it is passed as a reference to the control itself, which can then be used without first calling getElementByID.

I can't seem to find this behavior anywhere, is it a browser specific thing or a .net thing or what?

I am setting up a call like this in code ...

protected void Page_Load(object sender, EventArgs e)
        {
            Button1.Attributes.Add("onClick", string.Format("showvalue({0})",  TextBox1.ClientID));
        }

      

I am worried that this may not work in older versions of IE. Thank.

+1


source to share


2 answers


Add quotes around your id value when generating JS code like: string.Format ("showvalue ( ' {0} ' )", TextBox1.ClientID)



Without quotes, showValue gets an instance of a global variable with your ClientID name, which is usually the DOM element that your control is controlling.

+4


source


If you pass it in single quotes and use getElementByID, it should work in all browsers, which is the "normal" way.



I'm not sure why this method works the way you say. Does it work in Firefox too?

0


source







All Articles