SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Wizard gold trophysilver trophybronze trophy dc dalton's Avatar
    Join Date
    Nov 2004
    Location
    Right behind you, watching, always watching.
    Posts
    5,431
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Having a MAJOR JS BRAIN#$% - need eyes

    Im just not having a good day here today ..... Im doing some dynamic highlighting of form fields and Ive done this about a HUNDRED TIMES BEFORE but for some reason I cant see what Im doing wrong .....

    Heres the call to the function:

    Code:
    highlightFields(new Array('dlr_name','dlr_address','dlr_zipcode','dlr_areacode','dlr_phone_1','dlr_phone_2','dlr_email','dlr_license_no','dlr_license_state','dlr_license_exp_month','dlr_license_exp_year','readTerms'));
    here's the function:

    Code:
    function highlightFields(myArray)	 {
    	for (var x = 0 ; x < myArray.length ; x++)	{
    		alert("in function array is: "+myArray.length+"element is: "+myArray[x]);
    		document.getElementById(myArray[x]).style.backgroundColor = '#F9F11D';
    		}
    	}
    and before you ask, yes all the fields have the correct IDS in place, here's just one:

    <input type='text' name='dlr_name' size='30' id='dlr_name' maxlength='50' />

    Im getting a "document.getElementById(myArray[x]) has no properties javascript error and have been staring at this FOR AN HOUR!

    You'll notice I even have an alert in the function and it DOES show the correct ID in it and the correct length to the array .......

    Anyone see what Im missing cause obviously MY brain isnt working 100% today!

    BTW, Ignore that space in the code I pasted up here (mo nth).... thats the BB doing that, its not like that in my code..

  2. #2
    SitePoint Enthusiast
    Join Date
    Jan 2005
    Location
    Nelson, NZ
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <html>
    <head>
    <title>dc dalton highlighting</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta name = "GENERATOR" Content = "ScrypTik V1.09">
    <script type = "text/javascript" language = "Javascript">
    <!-- Hide from older browsers;
    function highlightFields(myArray) {
    for (var x = 0 ; x < myArray.length ; x++) {
    // alert("in function array is: "+myArray.length+" element is: "+myArray[x]+" x= "+x);
    document.getElementById(myArray[x]).style.backgroundColor = '#F9F11D';
    }
    }
    // end hide -->
    </script>
    </head>
    <body onload="highlightFields(new Array('dlr_name','dlr_address','dlr_zipcode','dlr_areacode',
    'dlr_phone_1','dlr_phone_2','dlr_email','dlr_license_no','dlr_license_state',
    'dlr_license_exp_month','dlr_license_exp_year','readTerms'));">
    <!-- Insert HTML here -->

    <input type='text' name='dlr_name' size='30' id='dlr_name' maxlength='50' /><br>
    <input type='text' name='dlr_address' size='30' id='dlr_address' maxlength='50' /><br>
    <input type='text' name='dlr_zipcode' size='30' id='dlr_zipcode' maxlength='50' /><br>
    <input type='text' name='dlr_areacode' size='30' id='dlr_areacode' maxlength='50' /><br>
    <input type='text' name='dlr_phone_1' size='30' id='dlr_phone_1' maxlength='50' /><br>
    <input type='text' name='dlr_phone_2' size='30' id='dlr_phone_2' maxlength='50' /><br>
    <input type='text' name='dlr_email' size='30' id='dlr_email' maxlength='50' /><br>
    <input type='text' name='dlr_license_no' size='30' id='dlr_license_no' maxlength='50' /><br>
    <input type='text' name='dlr_license_state' size='30' id='dlr_license_state' maxlength='50' /><br>
    <input type='text' name='dlr_license_exp_month' size='30' id='dlr_license_exp_month' maxlength='50' /><br>
    <input type='text' name='dlr_license_exp_year' size='30' id='dlr_license_exp_year' maxlength='50' /><br>
    <input type='text' name='readTerms' size='30' id='readTerms' maxlength='50' /><br>

    </body>
    </html>

    This works in firefox, I think the problem was the javascript ran before the html items were created!!!!

    Cheers Paul

  3. #3
    SitePoint Wizard gold trophysilver trophybronze trophy dc dalton's Avatar
    Join Date
    Nov 2004
    Location
    Right behind you, watching, always watching.
    Posts
    5,431
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You know what YOUR EXACTLY RIGHT, dammit! I told you my brain wasnt working! I have that code right before I create the form elements ..... should have been after!

    Thanks so much for that "kick in the head"

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just a recommendation: never put an onload event in the body tag unless you purposely want to create problems for someone who might use your script.

  5. #5
    SitePoint Wizard gold trophysilver trophybronze trophy dc dalton's Avatar
    Join Date
    Nov 2004
    Location
    Right behind you, watching, always watching.
    Posts
    5,431
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 7stud
    Just a recommendation: never put an onload event in the body tag unless you purposely want to create problems for someone who might use your script.
    what do you mean 7stud ......

    Im actually not using the onload event here, I actually have to call the function to highlight the fields from a piece of js down in the body... (hence where the error came from) ....... reason being is the way this app works Im already WAY past any of my files that allow me access to the entire head or the body tag .... I dont know if I have errors until Im past that point. I could rewrite a TON of code to fix it but its really not worth all that pain!

  6. #6
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what do you mean 7stud ......
    Someone who wants to add a function to the onload event can't do it unless they have access to the file with the <body> tag and they have permission to change it. Every so often someone posts that their boss won't allow them to change the file that contains the <body> onload, yet they need to write a script that is to be embeded in the page that uses the onload event to execute a function. If you try to add a function to window.onload, the body onload overwrites it. If instead, the original function is assigned to window.onload, you can always add another function to the onload event like this:
    Code:
    if(window.onload) var old = window.onload;
    window.onload = function()
    {
         if(old) old(); 
         new_func();
    }

  7. #7
    SitePoint Wizard gold trophysilver trophybronze trophy dc dalton's Avatar
    Join Date
    Nov 2004
    Location
    Right behind you, watching, always watching.
    Posts
    5,431
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    AH I SEE! ..... dont have to worry about that here ......... IM the boss!

    but I do see your point!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •