how to tell what form to use in javascript

P

Paul

I have a javascript that I am setting a value of a textbox in and have the
following
document.forms[0].textbox1.value = total;
I get a run time error null value but I think the problem is the form index
0. The web application has about 20 forms, is there anyway to tell what the
index should be?
thanks.
 
N

Nathan Sokalski

Is this an page created by ASP.NET (*.aspx)? If it is, it should only have
one form. Also, if the textbox was created by an ASP.NET TextBox server
control (I'm assuming it was since this is an ASP.NET newsgroup), then not
only will the id attribute in the generated HTML be different than the one
in your *.aspx source file, but I would recommend using
document.getElementById(...).value instead (this is what is normally used,
and it does not require an index). My suggestions are based on the
assumption this is an ASP.NET *.aspx Page, if this assumption is incorrect
we may need to see your source code to determine your problem. Good Luck!
--
Nathan Sokalski
(e-mail address removed)
http://www.nathansokalski.com/

Mark Rae said:
I have a javascript that I am setting a value of a textbox in and have the
following
document.forms[0].textbox1.value = total;
I get a run time error null value but I think the problem is the form
index
0. The web application has about 20 forms, is there anyway to tell what
the
index should be?

Can't you refer to the form by its name...?
http://www.devguru.com/Technologies/ecmascript/quickref/doc_forms.html
 
P

Paul

thanks for the responses, below is the code, it is aspx pages. I created a
small sample web app with only 3 webpages and it works fine but when
integrating it into a large web app I ran into the following problems. When
I call the javascript function (calcBalancenew) and try to pass in the
textbox values there is a java error txbx1 not defined. Note the first
attribute works since I am just passing in integers but the others fail. The
second problem is that in the java script function (calcBalancenew) where I
try to write the results to textbox 4 with

document.forms[0].txbx4.value = total;
I get the error document.forms.0.txbx4 is null or not an object,
so it looks like it can not find it, so thinking I am not accessing the form
correctly.

The large web app uses a master page and all other pages are content of the
master page when it gets loaded in.
I have a simple java function that just adds values in text boxes 1 through
4 and displays the results in text box 4.
In the code behind to use the onblur event with asp.net textbox controls for
4 textboxes I have

txbx1.Attributes["onblur"] = "calcBalancenew(7,1,2,3)";
txbx2.Attributes["onblur"] =
"calcBalancenew(txbx1.value,txtbx2.value,txbx3.value,txbx4.value)";
txbx3.Attributes["onblur"] =
"calcBalancenew(txbx1.value,txbx2.value,txbx3.value,txbx4.value)";
txbx4.Attributes["onblur"] =
"calcBalancenew(txbx1.value,txbx2.value,txbx3.value,txbx4.value)";


--
Paul G
Software engineer.


Nathan Sokalski said:
Is this an page created by ASP.NET (*.aspx)? If it is, it should only have
one form. Also, if the textbox was created by an ASP.NET TextBox server
control (I'm assuming it was since this is an ASP.NET newsgroup), then not
only will the id attribute in the generated HTML be different than the one
in your *.aspx source file, but I would recommend using
document.getElementById(...).value instead (this is what is normally used,
and it does not require an index). My suggestions are based on the
assumption this is an ASP.NET *.aspx Page, if this assumption is incorrect
we may need to see your source code to determine your problem. Good Luck!
--
Nathan Sokalski
(e-mail address removed)
http://www.nathansokalski.com/

Mark Rae said:
I have a javascript that I am setting a value of a textbox in and have the
following
document.forms[0].textbox1.value = total;
I get a run time error null value but I think the problem is the form
index
0. The web application has about 20 forms, is there anyway to tell what
the
index should be?

Can't you refer to the form by its name...?
http://www.devguru.com/Technologies/ecmascript/quickref/doc_forms.html
 
P

Paul

I was able to set the textbox value by using the getElementById as suggested,
still have the other issue though where it does not recognize the arguments
being passed into the onblur event. This is in the C# code behind to create
the onblur events for the web controls.

txbx2.Attributes["onblur"] =
"calcBalancenew(txbx1.value,txtbx2.value,txbx3.value,txbx4.value)";
.
document.getElementById ('<%=txbx4.ClientID%>').value=total;
--
Paul G
Software engineer.


Paul said:
thanks for the responses, below is the code, it is aspx pages. I created a
small sample web app with only 3 webpages and it works fine but when
integrating it into a large web app I ran into the following problems. When
I call the javascript function (calcBalancenew) and try to pass in the
textbox values there is a java error txbx1 not defined. Note the first
attribute works since I am just passing in integers but the others fail. The
second problem is that in the java script function (calcBalancenew) where I
try to write the results to textbox 4 with

document.forms[0].txbx4.value = total;
I get the error document.forms.0.txbx4 is null or not an object,
so it looks like it can not find it, so thinking I am not accessing the form
correctly.

The large web app uses a master page and all other pages are content of the
master page when it gets loaded in.
I have a simple java function that just adds values in text boxes 1 through
4 and displays the results in text box 4.
In the code behind to use the onblur event with asp.net textbox controls for
4 textboxes I have

txbx1.Attributes["onblur"] = "calcBalancenew(7,1,2,3)";
txbx2.Attributes["onblur"] =
"calcBalancenew(txbx1.value,txtbx2.value,txbx3.value,txbx4.value)";
txbx3.Attributes["onblur"] =
"calcBalancenew(txbx1.value,txbx2.value,txbx3.value,txbx4.value)";
txbx4.Attributes["onblur"] =
"calcBalancenew(txbx1.value,txbx2.value,txbx3.value,txbx4.value)";


--
Paul G
Software engineer.


Nathan Sokalski said:
Is this an page created by ASP.NET (*.aspx)? If it is, it should only have
one form. Also, if the textbox was created by an ASP.NET TextBox server
control (I'm assuming it was since this is an ASP.NET newsgroup), then not
only will the id attribute in the generated HTML be different than the one
in your *.aspx source file, but I would recommend using
document.getElementById(...).value instead (this is what is normally used,
and it does not require an index). My suggestions are based on the
assumption this is an ASP.NET *.aspx Page, if this assumption is incorrect
we may need to see your source code to determine your problem. Good Luck!
--
Nathan Sokalski
(e-mail address removed)
http://www.nathansokalski.com/

Mark Rae said:
I have a javascript that I am setting a value of a textbox in and have the
following
document.forms[0].textbox1.value = total;
I get a run time error null value but I think the problem is the form
index
0. The web application has about 20 forms, is there anyway to tell what
the
index should be?

Can't you refer to the form by its name...?
http://www.devguru.com/Technologies/ecmascript/quickref/doc_forms.html
 
P

Paul

ok thanks it worked! It was using the actual text for the client id instead
of retrieving the client id when the page was rendered.
--
Paul G
Software engineer.


Mark Rae said:
I was able to set the textbox value by using the getElementById as
suggested,
still have the other issue though where it does not recognize the
arguments
being passed into the onblur event. This is in the C# code behind to
create
the onblur events for the web controls.

txbx2.Attributes["onblur"] =
"calcBalancenew(txbx1.value,txtbx2.value,txbx3.value,txbx4.value)";
.
document.getElementById ('<%=txbx4.ClientID%>').value=total;

How about something like:

"calcBalancenew(" +
txbx1.ClientID + ".value," +
txbx2.ClientID + ".value," +
txbx3.ClientID + ".value," +
txbx4.ClientID + ".value)";

The syntax may not be 100% accurate, but you get the idea...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,218
Latest member
JolieDenha

Latest Threads

Top