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 2 of 2
  1. #1
    Regular Coder
    Join Date
    Dec 2006
    Posts
    306
    Thanks
    24
    Thanked 0 Times in 0 Posts

    onLoad and onchange affect each other

    Hi;
    I'd like to ask why the onLoad and onchange affect eachother ? How i can avoide that ?

    <body onLoad="checkCookie()">
    and
    <select name="province_state_territories" id="state" onchange="setCities();">


    when i use the above at the same time, the search function have not another option to choise. It means the option for selection
    have not values.

    Could anyone help me please, Thanks.

    Because i want to use the "<body onLoad="checkCookie()">" to check my cookie and decide which Language i will use.



    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <META HTTP-EQUIV="content-type" CONTENT="text/html; charset=utf-8">
    
    <script type="text/javascript">
    
    // State lists
    
    var states = new Array();
    states['Australia'] = new Array('New South Wales');
    
    
    // City lists
    var cities = new Array();
    cities['Australia'] = new Array();
    cities['Australia']['New South Wales'] = new Array('Sydney');
    
    
    function setStates() {
     cntrySel = document.getElementById('country');
     stateList = states[cntrySel.value];
     changeSelect('state', stateList, stateList);
     setCities();
    }
    
    function setCities() {
     cntrySel = document.getElementById('country');
     stateSel = document.getElementById('state');
     cityList = cities[cntrySel.value][stateSel.value];
     changeSelect('city', cityList, cityList);
    }
    
    function changeSelect(fieldID, newOptions, newValues) {
     selectField = document.getElementById(fieldID);
     selectField.options.length = 0;
     for (i=0; i<newOptions.length; i++) {
       selectField.options[selectField.length] = new Option(newOptions[i], newValues[i]);
     }
    }
    
    // Multiple onload function created by: Simon Willison
    // http://simonwillison.net/2004/May/26/addLoadEvent/
    function addLoadEvent(func) {
     var oldonload = window.onload;
     if (typeof window.onload != 'function') {
       window.onload = func;
     } else {
       window.onload = function() {
         if (oldonload) {
           oldonload();
         }
         func();
       }
     }
    }
    
    addLoadEvent(function() {
     setStates();
    });
    
    </script>
    </head>
    
    <body onLoad="checkCookie()">
    
    <form name="search_form" id="sf" action="search_p1.php" method="get">
    <div>
    <span class="search_form">Country:</class>
    <select name="country" id="country" onchange="setStates();">
    
     <option value="Australia">Australia</option>
    
    </select>
    
    <span class="search_form">State:</class>
    <select name="province_state_territories" id="state" onchange="setCities();">
     <option value="">please choose a state</option>
    </select>
    
    <br>
    <span class="search_form">City:</class>
    <select name="city"  id="city">
     <option value="">please choose a city</option>
    </select>
    
    
    
    <input type="submit" name="search_submit" value="search"></div>
    </form>
    
    </body>
    </html>

  • #2
    Regular Coder
    Join Date
    Dec 2006
    Posts
    306
    Thanks
    24
    Thanked 0 Times in 0 Posts
    Thanks for come here, i have known the solution already.

    replace

    PHP Code:
    Code:
    addLoadEvent(function() {
     
    setStates();
    });
    with

    Code
    :
    addLoadEvent(function() {
     
    setStates();
     
    checkCookie();
    }); 


  •  

    Posting Permissions

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