Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    The onload function is not setting value.

    I am having problems with setting a value with onload. Here is an example of the code.

    Code:
    <form method="get" class="searchform" action=theAction" onload="if('' == '') {document.getElementById('searchinput').value='Type Search Query';} else {document.getElementById('searchinput').value='june';}">
    <input class="searchinput" name="s" type="text">
    <input class="searchbutton" value="Go" type="submit"></form>
    I know you guys might be thinking that the if condition is hard coded, and the if will never change. In the real code, php changes the first value of the if's condition.

    I think the above code should always set the value 'Type Search Query', in the box. However, it does not set any value.

    Can someone please explain what is going on? Also, is there another, btter way to do something like this a different way?

    Thanks,
    Grae

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,252
    Thanks
    203
    Thanked 2,557 Times in 2,535 Posts
    form...onload is not valid javascript. And as the input field doesn't exist until the page is loaded you need to place the code to set the value of the field into a function and call the function from the global event handler like this:


    Try this:-

    Code:
    <script type = "text/javascript">
    function init() {
    if ('' == '') {  // whatever condition applies
    document.getElementById('sinput').value = 'Type Search Query';
    } 
    else {
    document.getElementById('sinput').value='june';
    }
    }
    window.onload = init; 
    </script>
    
    <input type = "text" class="searchinput" name = "sinput" id = "sinput"


    Quizmaster: Who sang the theme tune to the James Bond film "Goldfinger"?
    Contestant: Shirley Temple.

  • #3
    New to the CF scene
    Join Date
    May 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks. I will consider that. Another person suggested, just making the if statement in the php code, and handling it that way. I htink I might go with that. Thanks, I will use this as a back up plan I guess.

    Thanks,
    Grae


  •  

    Posting Permissions

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