473,395 Members | 1,484 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,395 software developers and data experts.

document.getElementById Problems

Hi all, I have a very simple HTML page that coontains a form and some various text and hidden fields. When the page loads I want to read in the query string and update some of the text fields with the query string values. Here is the code I have:
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" >
  3. <head>
  4.     <title>New Contact</title>
  5. </head>
  6. <script src="mtcFormAPI.js" type="text/javascript" language="javascript">
  7. </script>
  8. <body >
  9. <script language="javascript">
Expand|Select|Wrap|Line Numbers
  1.     var qsParm = new Array();
  2.     qsParm['Campaign_CRM'] = null;
  3.     qsParm['Company'] = null;
  4.     function qs() {
  5.     var query = window.location.search.substring(1);
  6.     var parms = query.split('&');
  7.     for (var i=0; i<parms.length; i++) {
  8.         var pos = parms[i].indexOf('=');
  9.         if (pos > 0) {
  10.             var key = parms[i].substring(0,pos);
  11.             var val = parms[i].substring(pos+1);   
  12.             qsParm[key] = val;
  13.         }
  14.     }
  15.     return true;
  16.  
  17. var test = qs();
  18.  
  19. var campaign = document.getElementById('Campaign__CRM');
  20.  
  21. if (campaign) {
  22.     alert("testa");
  23.    campaign.value = qsParm['Campaing__CRM'];
  24. }
  25.  
  26. var company = document.getElementById('Company');
  27.  
  28. if (company != null) {
  29.     alert(qsParm['Company']);
  30.     company.value = qsParm['Company'];
  31. }
Expand|Select|Wrap|Line Numbers
  1. </script>
  2. <form action="Contact3.htm" method="post" name="myform" id="myform">
  3.     <table>
  4.    <tr><td>First Name </td><td><input id="FirstName" name="FirstName" type="text" /></td></tr>
  5.    <tr><td>Last Name</td><td><input id="LastName" name="LastName" type="text" isRequired="true" /></td></tr>
  6.    <tr><td>Email Address</td><td><input id="EmailAddress" name="EmailAddress" type="text" isRequired="true" /></td></tr>
  7.    <tr><td>Company Name</td><td><input id="Company" name="Company" type="text" isRequired="true" /></td></tr>
  8.  
  9.     </table>          
  10.     <input id="Campaign__CRM" type="hidden" name="Campaign__CRM" value="" />  
  11.     <input id="Button1" type="button" value="Submit" onclick="javascript:mtcSubmitForm(document.myform);" />
  12.  </form>
  13. </body>
  14. </html>
The query string gets parsed correctly, but the two getElementByID calls return null. Am I missing something basic?
Sep 20 '07 #1
2 1444
epots9
1,351 Expert 1GB
the problem is that your calling getElementById but those elements aren't loaded yet so u get errors, what u want to do it put your javascript in a function and change the body to:
[html]
<body onload="myFunction();">
[/html]

good luck
Sep 20 '07 #2
pbmods
5,821 Expert 4TB
Heya, ThePope. Welcome to TSDN!

Please use CODE tags when posting source code:

[CODE=javascript]
JavaScript code goes here.
[/CODE]
Sep 20 '07 #3

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

Similar topics

6
by: David List | last post by:
I'm having a problem using different properties of the document object in the example javascripts in my textbook with browsers that identify themselves as using the Mozilla engine. One example of...
12
by: Kepler | last post by:
How do you get the height of the client browser in IE? Both document.body.clientHeight and document.body.offsetHeight return the height of the document. If the page is long and there's a vertical...
12
by: lawrence | last post by:
The following function correctly makes everything invisible but then fails to turn the one chosen DIV back to visible. I imagine I'm getting the syntax of the variable wrong? I've tried this with...
4
by: lawrence | last post by:
Can anyone tell me why this code works in Netscape 7.1 but not in IE??? <SCRIPT type='text/javascript'> function makeVisible(nameOfDiv) {...
7
by: PaulB | last post by:
Good Morning everybody, I'm trying to adapt a tutorial script that will handle the behaviour of an "Expanding/Contracting" site-navigation menu. The code that seems to handle the expansion and...
2
by: Earl Teigrob | last post by:
I am trying to build a custom control to wrap my smart navigation implimention (not microsofts 'cause it has problems) The follow code works fine when the onclick and onload events are defined in...
7
by: Andrea | last post by:
Hi there - I'm hoping someone can help me; I've been struggling with this for a few days! :-) I have a webpage that is comprised of many forms containing questions. As the user answers one...
8
by: Henrik Stidsen | last post by:
I am trying to access a table in an iframe via javascript. It sounds easy - but it won´t work... The iframe is added to the document via someContainerElement.innerHTML = "<iframe...>", it has...
0
acoder
by: acoder | last post by:
Problem document.getElementById() selects an element by its name. Browser Internet Explorer Example Relevant HTML code: <input name="test" type="text" value="test"> The Javascript code:
29
by: Nick | last post by:
I've seen a few frameworks use the following: function $(id) { return document.getElementById(id); } Then to use: $('something').innerHTML = 'blah'; I'm just trying to roll this out to my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.