swolff1978
swolff1978

Reputation: 1865

Pass asp control as parameter to javascript

I would like to have this function:

function count(inputObj, ouputObj)
{
   outputObj.value = inputObj.value.length;
}

And I would like to call it in this way:

<asp:TextBox ID="txtUSERGUID" runat="server" onkeyup="count(this, document.getElementById('<%=txtUSERGUIDLength.ClientID%>'));" onkeydown="count(this, document.getElementById('<%=txtUSERGUIDLength.ClientID%>'));" />

<asp:TextBox ID="txtUSERGUIDLength" runat="server" />

<asp:Label ID="lblUSERGUIDLength" runat="server" Text="chars" />

But I keep getting a javascript error that says: 'outputObj' is undefined. Can I call this function like this or am I going the completely wrong direction?

Upvotes: 0

Views: 6305

Answers (3)

Timbo
Timbo

Reputation: 4533

I don't believe you can insert parameters in this manner for server side controls.

You can set these parameters in your server side code:

txtUSERGUID.Attributes.Add("onkeyup", yourEventHandlerScript);

Upvotes: 0

David
David

Reputation: 34563

Check your page source, I think you'll find that the "onkeyup" event handler still has the server side code in it. Since you are setting a property on a server control, the "<%= %>" code is not executed. You'll have to move this to a separate javascript function if you want to do this.

Upvotes: 1

kemiller2002
kemiller2002

Reputation: 115488

You can't have this inside of a server tag it won't execute the server side code:

<%=txtUSERGUIDLength.ClientID%>

If you are going to do it do something like

<script language='javascript'>

var g_clientID = '<%=txtUSERGUIDLength.ClientID%>'

<script/>

then do:

onclick(this, document.getElementById(g_clientID))

Upvotes: 5

Related Questions