Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Please let me know if I'm misunderstanding the question, but I think I've dealt with something similar.</p> <p>Browsers all handle the change event differently and usually doesn't fire until the user clicks away from that field after updating it (by clicking somewhere else on the page and solidifying their change).</p> <p>My workaround has been to create a function that does the work for me and have that function run on click instead of change. To cover your bases (and not have dynamic data loading unnecessarily), I would go about it this way:</p> <ol> <li>Create global variables or object to track the state of each dropdown (you can store id, value, etc. - this basically alows you to check whether the dropdown. For default state, create all the variables and set their values to blank)</li> <li>Create a function that runs each time any of the fields are clicked and pass in the dropdown (either through ID, name or the actual jQuery object)</li> </ol> <p><strong>The JavaScript Portion</strong> </p> <pre><code>$(document).ready(function() { // Attach click function to princ $("select[name='princ']").click(function() { var value = $(this).val(); // Grab the value if (value != demo.globals.princ) { // Check if the value is the old value demo.checkDropdown('princ', 'princ2', value); // If it's a new value, grab the selected value and run the checkDropdown function demo.globals.princ = value; // Set the global value $('#status').text("loading new values for princ " + value); } else { $('#status').text("princ values already loaded " + value); // Unnecessary but used to output the status of the clicks } }); // Attach click function to princ2 $("select[name='princ2']").click(function() { var value = $(this).val(); if (value != demo.globals.princ2) { demo.checkDropdown('princ2', 'princ3', value); demo.globals.princ2 = value; $('#status').text("loading new values for princ2 " + value); } else { $('#status').text("princ2 values already loaded " + value); } }); }); // Create a global object to avoid function collision var demo = {}; // Create global variables demo.globals = { princ: 0, princ2: 0 } // Dropdown population function demo.checkDropdown = function(dropDownName, nextDropDown, value) { // Check if it's already populated with the correct data, otherwise load it // You can also load dynamic data here via AJAX and update the select field var dropDownHtml = "&lt;option value='1'&gt;1&lt;/option&gt;"; dropDownHtml += "&lt;option value='2'&gt;2&lt;/option&gt;"; dropDownHtml += "&lt;option value='3'&gt;3&lt;/option&gt;"; $("select[name='" + nextDropDown + "']").html(dropDownHtml); } </code></pre> <p><strong>The HTML</strong> </p> <pre class="lang-html prettyprint-override"><code>&lt;body&gt; &lt;form&gt; &lt;div id="status"&gt;Page loaded&lt;/div&gt; &lt;select name="princ"&gt; &lt;option value="0"&gt;Please select one&lt;/option&gt; &lt;option value="1"&gt;1&lt;/option&gt; &lt;option value="2"&gt;2&lt;/option&gt; &lt;option value="3"&gt;3&lt;/option&gt; &lt;/select&gt; &lt;select name="princ2"&gt; &lt;/select&gt; &lt;select name="princ3"&gt; &lt;/select&gt; &lt;/form&gt; &lt;/body&gt; </code></pre> <p>I really hope this helps you out. It's a little more than what you're asking for, but it is a complete, working example of how to get dropdown values on change.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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