[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

passing textbox id in javascript

Posted on 2006-05-31
4
Medium Priority
?
330 Views
Last Modified: 2008-03-04
I'm doing the following on the server side

oButton.Attributes.Add("onclick", "MyFunction(this.ID)")


on the client side I have the following javascript function

MyFunction(theId)
{

alert(theId);

}

But I get  an error Object expected. How can I pass the textboxes id in dynamicall because they are part of a user control and the names are dynamic. Thanks.

0
Comment
Question by:dotnet22
4 Comments
 
LVL 27

Expert Comment

by:Sammy Ageil
ID: 16800053
<script type="text/javascript">
MyFunction(myId){
alert(document.getElementById(myId));
}
</script">
HTH
0
 
LVL 5

Expert Comment

by:Sam_Jayander
ID: 16804795
Hi,

use,
oButton.Attributes.Add("onclick", "MyFunction(this.id)")

while accessing the id-property in javascript you must use "id" and not "ID".

Regards,
--Sam.
0
 
LVL 9

Accepted Solution

by:
bele04 earned 2000 total points
ID: 16805956
Hi,

you might wanna try:

oButton.Attributes.Add("onclick", "MyFunction(\"" + oButon.ID + "\")")

or if that doesn't work try:

oButton.Attributes.Add("onclick", "MyFunction(\"" + oButon.ClientID + "\")")


hope it helps
-bele04-
0
 
LVL 3

Expert Comment

by:EtherZa
ID: 16806417
oButon.ID is not a safe value as it gets chaned by the compiler when the control is contained with another control. Use oButton.ClientID rather.

Better still is to use the following:

Code:
oButton.Attributes.Add("onclick", "MyFunction(this)")

or if you are using asp.net 2: <asp:button runat="server" id="btn" OnClientClick="javascript:MyFunction(this);" />

HTML:
<script type="text/javascript">
function MyFunction(obj)
{
  alert("Object id: " + obj.id) // will show you the id of the button that was clicked; alternatively "obj" is the button object to be manipulated.
}
</script>
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
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.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month19 days, 14 hours left to enroll

872 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