Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to run an onchange function when page is loaded if the selected box is already selected
    text
    copied!<p>Im running into a problem where i have an ajax driven page that is drawn when a user selects something from a simple drop down:</p> <pre><code>&lt;select id = "selectdepartment"&gt; &lt;option id = "default"&gt;Select an option...&lt;/option&gt; .... &lt;/select&gt; </code></pre> <p>and the remainder of the page is drawn using the jquery <code>.change()</code> :</p> <pre><code>$('#selectdepartment').change(function(){ }); </code></pre> <p>Which then runs some ajax to php script. everything works great, the problem is when i submit a form that was drawn with ajax (using <code>$_SERVER['PHP_SELF'];</code>), the data gets submited, the page reloads, and the page is cleared but the select box is still left where it was. The user has to move to a different option then back to the one the selected originally to re-fire the <code>.change()</code>. that sucks. </p> <p>I could fix this by passing a php variable in all of my forms, then checking to see the variable set on every page load and if it is draw the page parts then, but this would lead to pretty messy code and it's less than desirable. </p> <p>There has to be a way to do this with the jquery library, though my knowledge of the javascript language in general is not what i would like it to be. If anyone has any helpful hints please share, dont do it for me though, i wont learn that way :)</p> <p>edit: code with .trigger</p> <pre><code>$('#selectdepartment').change(function(){ var department = $('#selectdepartment').val(); var day = $('#data').data('day'); var month = $('#data').data('month'); var year = $('#data').data('year'); //alert (department); if(department === "Select an option..."){ $('.hiddenuntildepartmentisselected').css({"display":"none"}); } else{ $('.hiddenuntildepartmentisselected').css({"display":"block"}); } showpoints(department); drawpointstable(department, day, month, year); displaytheuseresforselecteddepartment(department, ''); $('#sendthedepartment').val(''+department+''); $('#hiddendepartmentidforaddinganewpoint').val(''+department+''); }).trigger('change');//end run functions </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload