SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Sep 2004
    Location
    London
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Change field value on page load

    Hi everyone

    Was wondering if some could help me with this cuz I'm pulling my hair out (Javascript newbie).

    I have a form (below) and I need to use Javascript to change the name of one of the hidden fields when the page loads. This updated value will then be added to the URL on form submit to tell our server that Javascript is enabled.

    I have been digging through some Javascript books I have and just need someone to help me put it all together. This is what I have so far:

    HTML Code:
    <head>
    <script type="text/javascript">
    function updateValue() {
    	var findField = document.getElementByID('js');
    	var findField.value = "on";
    }
    
    window.onload = updateValue;
    </script>
    </head>
    
    <body>
    <form action="http://www.xyz.com/filename.aspx?action=[define]&text=[text]&searchlang=[searchlang]&deflang=[deflang]&product=[product]&searchtype=[searchtype]&js=[js]" method="get">
    <input type="hidden" name="action" value="define" />
    <input type="hidden" name="searchlang" value="english" />
    <input type="hidden" name="deflang" value="english" />
    <input type="hidden" name="product" value="xxx" />
    <input type="hidden" name="searchtype" value="stemming" />
    <input type="hidden" name="js" value="off" id="js" />
    <label for="text">Search</label>
    <input type="text" size="20" id="text" name="text" />
    <input type="submit" value="search" size="5" class="button" />
    </form>
    Okay, so as you can see, I'm trying to change the value of the input with the an ID of "js"

    Any help would be greatly appreciated.

    Thanks
    Mike

  2. #2
    SitePoint Evangelist hexburner's Avatar
    Join Date
    Jan 2007
    Location
    Belgium
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're trying to change the value I assume.
    You got it almost.
    Try the window.addEvent function written by Scott Andrew to attach an onload event properly.
    FOR SALE: 1 set of morals, never used, will sell cheap

  3. #3
    SitePoint Member
    Join Date
    Sep 2004
    Location
    London
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    Thanks for your reply. I found the addEvent function in the forum and have tried to customise it, but it isn't working. The script I have put together is below. Please can you have a look and let me know where I am going wrong.

    HTML Code:
    window.addEvent=function(e,ev,f,c){if(e.addEventListener){e.addEventListener(ev,f,c);}else if(e.attachEvent){var r=e.attachEvent('on'+ev,f);return r;}else{e['on'+ev]=f;}};
     
    function updateValue(){ 
    	var findField = document.getElementByID('js');
    	var findField.value = "on";
    }
    
    window.addEvent(window, "load", updateValue, false);
    Thanks
    Mike

  4. #4
    SitePoint Evangelist hexburner's Avatar
    Join Date
    Jan 2007
    Location
    Belgium
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You've got some syntax errors my friend

    Code JavaScript:
    window.addEvent=function(e,ev,f,c){if(e.addEventListener){e.addEventListener(ev,f,c);}else if(e.attachEvent){var r=e.attachEvent('on'+ev,f);return r;}else{e['on'+ev]=f;}};
     
    function updateValue(){ 
        var findField = document.getElementById('js'); // JS is case-sensitive
        findField.value = "on"; // var not needed, because findField was already defined
    }
     
    window.addEvent(window, "load", updateValue, false);
    FOR SALE: 1 set of morals, never used, will sell cheap

  5. #5
    SitePoint Member
    Join Date
    Sep 2004
    Location
    London
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey hexburner

    Thanks so much for your help. Was so frustrating to almost have been there but not quite. Gonna spend some more time in my Javascript books.

    Thanks again for your help.

    Mike


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
  •