Daniel Robinson
Daniel Robinson

Reputation: 653

run an onChange on load

I have this triple dropdown which works great from

http://roshanbh.com.np/2008/01/populate-triple-drop-down-list-change-options-value-from-database-using-ajax-and-php.html

Is it possible to run the function getState below on load as well as having its current onChange? This is for an edit page so some of the dropdowns may have values that need to be already selected.

I have modified the query's to do what I wish in the external pages but I think the first box needs running when the page loads to make the external query's work properly.

I have tried adding

 jQuery(document).ready(function() {
jQuery('#networks').trigger('change');
 });

but I think that is the wrong type of loader

 function getXMLHTTP() { //function to return the xml http object
    var xmlhttp=false;  
    try{
        xmlhttp=new XMLHttpRequest();
    }
    catch(e)    {       
        try{            
            xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(e){
            try{
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch(e1){
                xmlhttp=false;
            }
        }
    }

    return xmlhttp;
}


function getState(network) {        

    var strURL="findfolder.php?network="+network+"&perant="+<?php echo $row_rs_doc['parentid']; ?>;
    var req = getXMLHTTP();

    if (req) {

        req.onreadystatechange = function() {
            if (req.readyState == 4) {
                // only if "OK"
                if (req.status == 200) {                        
                    document.getElementById('folderdiv').innerHTML=req.responseText;                        
                } else {
                    alert("There was a problem while using XMLHTTP:\n" + req.statusText);
                }
            }               
        }           
        req.open("GET", strURL, true);
        req.send(null);
    }       
}

and my first select list

     <select  name="network" onchange="getState(this.value)">
  <?php
       do {  
    ?>
    <option value="<?php echo $row_rs_net['idnetworks']?>"<?php if (!(strcmp($row_rs_net['idnetworks'], $row_rs_doc['network']))) {echo "selected=\"selected\"";} ?>><?php echo $row_rs_net['netname']?></option>
    <?php
      } while ($row_rs_net = mysql_fetch_assoc($rs_net));

    ?>
   </select>

Upvotes: 0

Views: 430

Answers (1)

iambriansreed
iambriansreed

Reputation: 22241

Option 1

Change:

<select name="network" onchange="getState(this.value)">

to:

<select id="network" name="network" onchange="getState(this.value)">

Option 2

Change:

jQuery('#networks').trigger('change');

to:

jQuery('[name="networks"]').trigger('change');

Upvotes: 3

Related Questions