0

I have following Javascript line in my ASP.NET web app:

document.getElementById('<%=myTextBox[0].ClientID %>').value = "test";

how can I access myTextBox elements? for instance I want to change 5th element of this server side array, I want to pass a server side parameter to my function, how can I do it?

for instance:

server side:
ddl.Attributes.Add("onChange", "return OnFoodChange(this,'" + i + "');");


javascript function:
function OnFoodChange(myCmb,myIndex)

using document.getElementById('<%=myTextBox[myIndex].ClientID %>').value = "test"; gives me error, it says that myIndex is not defined, I think because myIndex is used like a server side parameter, how can I solve it?

it is my full JavaScript function:

   function OnFoodChange(myCmb,myIndex) {
               //alert('5');
try{
               var q = document.getElementById('<%= HFFoodPrice.ClientID %>').value.toString();
           var q2 = q.split(';');
           var index = 0;
           //alert(myCmb.selectedIndex.toString());

           //var e = document.getElementById(myCmb);
           var strUser = myCmb.options[myCmb.selectedIndex].value;

           document.getElementById('<%=myTextBox[0].ClientID %>').value = strUser;

           for (var j = 0; j < q2.length; j++) {
               if (q2[j] != '') {
                   var q3 = q2[j].split(',');
                   {

                   }
               }
           }
           }
           catch(err)
           {
           alert(err.message);
           }
       }
2
  • 1
    can you post some detailed code so that I can help you better. Commented Jul 9, 2012 at 6:03
  • Are you sure your have a server side array of Textbox called myTextbox? You need to elaborate further on what you are trying to do and how you are trying to do it. Right now there are just bits and pieces of information to go off of. Commented Jul 9, 2012 at 6:11

2 Answers 2

2

Send this control id of textbox instead of sending index as asp.net control id might not be as you expect,

In Code behind

ddl.Attributes.Add("onChange", "return OnFoodChange(this,'" + myTextBox[i].ClientID + "');");

In HTML

function OnFoodChange(myCmb,myTextBoxId) {
 currentTextBox = document.getElementById(myTextBoxId);
//Your code...
}
Sign up to request clarification or add additional context in comments.

15 Comments

Do you assign id to every TextBox you have in myTextBox?
You have to assign id to TextBox before you add it to array. TextBox t = new TextBox(); t.ID = "SomePrefixIfYouWant" + UniqueNumberMightBeLoopVariable.ToString(); myTextBox.Add(t);
You have to reflect that thing over here too ddl.Attributes.Add("onChange", "return OnFoodChange(this,'" + i + "');");
You should not send it, you need to send the with the same naming convention you used for assigning id to TextBoxes with something like "SomePrefixIfYouWant" + UniqueNumberMightBeLoopVariable.ToString();
okay then view source of page in browser and see what ids you have with textboxes?
|
1

You are trying to Mix Server side with Client side. it does not work like this. you need to transfer your server array to javascript on server then you will be able access its index on clien side.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.