473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

getElementsByName() generates undefined ouput value in a textbox

rrocket
116 100+
I am passing a value through query string to a popup and then trying to pass it back to the main page... I store the value in a hidden input box
Expand|Select|Wrap|Line Numbers
  1. <input type="text" name="FromZip" id="FromZip" value="<% response.write(Request.QueryString("txtZipFr")) %>" />
and am passing it back through query string to the main page with
Expand|Select|Wrap|Line Numbers
  1. <script language="JavaScript" type="text/javascript">
  2.    //<!--
  3.     //opener.location.reload(true);
  4.    //window.opener.location='bl_zip_sngl.asp?
  5.    var x;
  6.    x = document.getElementsByName("FromZip").value;
  7.    alert(x);
  8.     window.opener.location='bl_zip_sngl.asp?txtZipFr='+x
  9.     self.close();
  10.   // -->
  11. </script>
It gives me undefined as the output, but when I do it like this
Expand|Select|Wrap|Line Numbers
  1. <script language="JavaScript" type="text/javascript">
  2.    //<!--
  3.     //opener.location.reload(true);
  4.    //window.opener.location='bl_zip_sngl.asp?
  5.    var x;
  6.    document.getElementsByName("FromZip").value = 79797; //Only difference
  7.    x = document.getElementsByName("FromZip").value;
  8.    alert(x);
  9.     window.opener.location='bl_zip_sngl.asp?txtZipFr='+x
  10.     self.close();
  11.   // -->
  12. </script>
it at least returns a real value. Unfortunately I need to dynamically populate the box with what the user originally typed in.
Sep 4 '07 #1
14 8107
pbmods
5,821 Expert 4TB
Changed thread title to better describe the problem (did you know that threads whose titles do not follow the Posting Guidelines actually get FEWER responses?).
Sep 4 '07 #2
Logician
210 100+
x = document.getElementsByName("FromZip").value;
Try reading:
Expand|Select|Wrap|Line Numbers
  1. document.getElementsByName("FromZip")[0].value;
Otherwise you'll need to show the document.
Sep 4 '07 #3
rrocket
116 100+
I tried that, but it gave me a null or not an object error...
Sep 4 '07 #4
gits
5,390 Expert Mod 4TB
hi ...

when do you call the js? for all dom-methods the dom has to be read for use with js ... so to be sure you should do everything with the dom after the onload of the doc (body's onload) ... at this point the browser has loaded the docs-dom and you may use it reliably ...

kind regards
Sep 4 '07 #5
rrocket
116 100+
The javascript is called in the last statement just before everything is sent back to the main window... I am not sure that is what you are asking or not. I am mainly trying to pull just the values in the hidden text boxes. I know they are coming up since I have the input boxes set to text instead of hidden at the moment.

I can send the entire page if you think that will help.
Sep 4 '07 #6
gits
5,390 Expert Mod 4TB
it would help ... ;) ... but for simplicity ... and to simplify the problem for yourself you should try to create an example that contains the relevant things you have problems with ... during this simple step you might find out the problem for yourself and you will learn with every step you take in that direction ... i promise ... but you may also post the full code in case it is not TOO much ;))

kind regards

ps: try posts #3 solution again ... i think it should work ...
Sep 4 '07 #7
rrocket
116 100+
I pull most of the stuff that does not have anything to do with the issue... I can alway cut more out if needed.

Expand|Select|Wrap|Line Numbers
  1. <%
  2.     IF (request.Form("op") <> 1) THEN         
  3. %>
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6.     <head>
  7.         <title>Add A Description</title>
  8.             </head>
  9.     <body>
  10.  
  11.         <form action="AddNewDescription.asp" method="post" onsubmit="return validate_form()" name="newDescription">
  12.         <!--<form action="BL_ZIP_SNGL.asp" method="post" onsubmit="return validate_form()" name="BL_Zip_Sngl">-->
  13.             <table cellpadding="1" cellspacing="0" bgcolor="#000000" width="250">
  14.                 <tr>
  15.                     <td>
  16.                         <table width="100%" cellpadding="5" cellspacing="0" bgcolor="#FFFFFF">
  17.                             <tr bgcolor="#AC0636">
  18.                                 <td colspan="2" style="height: 15px"><span style="font-size: 9pt; font-family: @Arial Unicode MS; color: #ffffff;"><strong>
  19.                                         Add a description</strong></span></td>
  20.                             </tr>
  21.                             <tr bgcolor="#FFFFFF" style="color: #000000">
  22.                                 <td>
  23.                                     <strong><span style="font-size: 10pt; font-family: @Arial Unicode MS">Description:</span></strong></td>
  24.                                 <td style="text-align: right"><input type="text" name="description"/></td>
  25.                             </tr>
  26.                             <tr style="color: #000000">
  27.                                 <td>
  28.                                     <span style="font-size: 10pt; font-family: @Arial Unicode MS"><strong>NMFC #:</strong></span>
  29.                                  </td>
  30.                                 <td style="text-align: right"><input type="text" name="nmfc"/></td>
  31.                             </tr>
  32.                             <tr>  
  33.                                 <td width="100%" colspan="2" valign="top"><font size="2">If you need help with your NMFC # please call 1-866-633-8236</font></TD>                
  34.                             </TR>
  35.                             <tr bgcolor="#CCCCCC" style="color: #000000">
  36.                                 <td colspan="2" style="text-align: right"><input type="submit" value="Submit" /></td>
  37.                             </tr>
  38.                         </table>
  39.                     </td>
  40.                 </tr>
  41.             </table>
  42.             <div>zipfr:<%response.write(Request.QueryString("txtZipFr"))%></div>
  43.              <div>zipto:<%response.write(Request.QueryString("txtZipTo"))%></div>
  44.              <input type="text" name="FromZip" id="FromZip" value="<%response.write(Request.QueryString("txtZipFr"))%>" />
  45.              <input type="text" name="ToZip" id="ToZip" value="<%response.write(Request.QueryString("txtZipTo"))%>" />
  46.             <input type="hidden" name="op" value="1" />
  47.             <input type="hidden" name="id" value="<% response.write(Request.QueryString("id")) %>" />
  48.  
  49.         </form>
  50.     </body>
  51. </html>
  52. <%
  53.     ELSE
  54.         DescAdd()
  55. %>
  56. <script language="JavaScript" type="text/javascript">
  57.    //<!--
  58.     //opener.location.reload(true);
  59.    //window.opener.location='bl_zip_sngl.asp'
  60.    //var x = 0;
  61.    //document.getElementsByName('FromZip').value = 79797;
  62.    vFromZip = document.getElementsByName("FromZip").value;
  63.    vToZip = document.getElementsByName("ToZip").value;
  64.    alert(vToZip);
  65.     window.opener.location='bl_zip_sngl.asp?txtZipFr='+ vFromZip + '&txtZipTo=' + vToZip;
  66.     self.close();
  67.   // -->
  68. </script>
  69. <%
  70.     END IF
  71. %>
  72.  
  73.  
Sep 4 '07 #8
pbmods
5,821 Expert 4TB
Heya, Rocket.

Try post #3's solution.

document.getElementsByName() has "Elements" [plural] in it, which would imply that it is returning multiple Elements.
Sep 4 '07 #9
gits
5,390 Expert Mod 4TB
hi ...

as i said ... try posts #3 solution again:

Expand|Select|Wrap|Line Numbers
  1. document.getElementsByName('whatever_name');
returns a collection (list) of elements ... so

Expand|Select|Wrap|Line Numbers
  1. document.getElementsByName('whatever_name')[0].value;
returns the value of the first node with the given name ...

kind regards
Sep 4 '07 #10
rrocket
116 100+
It gave me a null or not an object error again...

I also tried using getElementById, but it gave me a object error. With or without the [0].
Sep 4 '07 #11
rrocket
116 100+
Here is a more concise version of what I posted before... Hopefully it helps.

Expand|Select|Wrap|Line Numbers
  1. <%sLocId  = Session("UserLocationID")
  2.  
  3. %>
  4.  
  5.  
  6. <html xmlns="http://www.w3.org/1999/xhtml">
  7.     <head>
  8.         <title>Add A Description</title>
  9.  
  10.     </head>
  11.     <body>
  12.  
  13.         <form action="AddNewDescription.asp" method="post" onsubmit="return validate_form()" name="newDescription">
  14.         <!--<form action="BL_ZIP_SNGL.asp" method="post" onsubmit="return validate_form()" name="BL_Zip_Sngl">-->
  15.             <table cellpadding="1" cellspacing="0" bgcolor="#000000" width="250">
  16.                 <tr>
  17.                     <td>
  18.                         <table width="100%" cellpadding="5" cellspacing="0" bgcolor="#FFFFFF">
  19.                             <tr bgcolor="#CCCCCC" style="color: #000000">
  20.                                 <td colspan="2" style="text-align: right"><input type="submit" value="Submit" /></td>
  21.                             </tr>
  22.                         </table>
  23.                     </td>
  24.                 </tr>
  25.             </table>
  26.             <div>zipfr:<%response.write(Request.QueryString("txtZipFr"))%></div>
  27.              <div>zipto:<%response.write(Request.QueryString("txtZipTo"))%></div>
  28.              <input type="text" name="FromZip" id="FromZip" value="<%response.write(Request.QueryString("txtZipFr"))%>" />
  29.              <input type="text" name="ToZip" id="ToZip" value="<%response.write(Request.QueryString("txtZipTo"))%>" />
  30.             <input type="hidden" name="op" value="1" />
  31.             <input type="hidden" name="id" value="<% response.write(Request.QueryString("id")) %>" />
  32.  
  33.         </form>
  34.     </body>
  35. </html>
  36.  
  37. <script language="JavaScript" type="text/javascript">
  38.    //<!--
  39.     //opener.location.reload(true);
  40.    //window.opener.location='bl_zip_sngl.asp'
  41.    //var x = 0;
  42.    //document.getElementsByName('FromZip').value = 79797;
  43.    vFromZip = document.getElementsByName("FromZip").value;
  44.    vToZip = document.getElementsByName("ToZip").value;
  45.    alert(vToZip);// values for vToZip and vFromZip come up as undefined
  46. alert(vFromZip);
  47.     window.opener.location='bl_zip_sngl.asp?txtZipFr='+ '54321' + '&txtZipTo=' + '12345'; //Hard coded values reach the next page without an issue
  48.     self.close();
  49.   // -->
  50. </script>
  51.  
Sep 4 '07 #12
pbmods
5,821 Expert 4TB
Heya, Rocket.

Try using document.getElementById().
Sep 4 '07 #13
rrocket
116 100+
I also tried using getElementById, but it gave me a object error. With or without the [0]. I have tried just about everything that I can think of and am totally out of ideas at this point... I just cannot seem to get this to run dynamically.... It works fine when I apply a hardcoded value to the getElementsByName variables.
Sep 4 '07 #14
gits
5,390 Expert Mod 4TB
hi ...

i tested it now ... and put the script into a function that is called onclick of the submit button:

Expand|Select|Wrap|Line Numbers
  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2.     <head>
  3.         <title>Add A Description</title>
  4.  
  5.     </head>
  6.     <body>
  7.  
  8.         <form action="AddNewDescription.asp" method="post" onsubmit="return validate_form()" 
  9.  
  10. name="newDescription">
  11.         <!--<form action="BL_ZIP_SNGL.asp" method="post" onsubmit="return validate_form()" 
  12.  
  13. name="BL_Zip_Sngl">-->
  14.             <table cellpadding="1" cellspacing="0" bgcolor="#000000" width="250">
  15.                 <tr>
  16.                     <td>
  17.                         <table width="100%" cellpadding="5" cellspacing="0" bgcolor="#FFFFFF">
  18.                             <tr bgcolor="#CCCCCC" style="color: #000000">
  19.                                 <td colspan="2" style="text-align: right"><input type="submit" 
  20.  
  21. value="Submit" onclick="return do_submit();"/></td>
  22.                             </tr>
  23.                         </table>
  24.                     </td>
  25.                 </tr>
  26.             </table>
  27.             <div>zipfr:<%response.write(Request.QueryString("txtZipFr"))%></div>
  28.              <div>zipto:<%response.write(Request.QueryString("txtZipTo"))%></div>
  29.              <input type="text" name="FromZip" id="FromZip" 
  30.  
  31. value="<%response.write(Request.QueryString("txtZipFr"))%>" />
  32.              <input type="text" name="ToZip" id="ToZip" 
  33.  
  34. value="<%response.write(Request.QueryString("txtZipTo"))%>" />
  35.             <input type="hidden" name="op" value="1" />
  36.             <input type="hidden" name="id" value="<% response.write(Request.QueryString("id")) %>" 
  37.  
  38. />
  39.  
  40.         </form>
  41.     </body>
  42. </html>
  43.  
  44. <script language="JavaScript" type="text/javascript">
  45. function do_submit() {
  46.    //<!--
  47.     //opener.location.reload(true);
  48.    //window.opener.location='bl_zip_sngl.asp'
  49.    //var x = 0;
  50.    //document.getElementsByName('FromZip')[0].value = 79797;
  51.    vFromZip = document.getElementsByName("FromZip")[0].value;
  52.    vToZip = document.getElementsByName("ToZip")[0].value;
  53.    alert(vToZip);// values for vToZip and vFromZip come up as undefined
  54. alert(vFromZip);
  55.     //window.opener.location='bl_zip_sngl.asp?txtZipFr='+ '54321' + '&txtZipTo=' + '12345'; //Hard coded values reach the next page without an issue
  56.     //self.close();
  57.   // -->
  58.   return false;
  59. }
  60. </script>
note:

Expand|Select|Wrap|Line Numbers
  1. vFromZip = document.getElementsByName("FromZip")[0].value;
  2. vToZip = document.getElementsByName("ToZip")[0].value;
  3.  
works in my test ... it returns the value of the textboxes.

kind regards

ps: the function-thing is only for my test, you don't need it ... and i avoided the close of the window ... outcommented
Sep 5 '07 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Pavils Jurjans | last post by:
Hello, I am building custom hashtable class, and thinking about value retrieval issues. The thing is, that sometimes the hashtable value may contain value null. If someone is reading this value...
3
by: VB Programmer | last post by:
In my ASPX page how do I get the .text value of a textbox that is in the ItemTemplate of a datalist (using HTML)? The textbox contains the quantity for a shopping cart item. The textbox is...
49
by: matty | last post by:
Hi, I recently got very confused (well that's my life) about the "undefined" value. I looked in the FAQ and didn't see anything about it. On...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.