I think we are all embarrassed. But quickly break your options.
After updating the question, it seems like the answer you can find is my last example. Please consider all other information as well, as this may help you determine the best process for your "Ultimate Goal."
First, you have a DOM Load event, as indicated in another answer. This will complete the page loading and will always be your first call to HEAD JavaScript. to learn more, see this API documentation .
Example
$(document).ready(function () { alert($('select').val()); }) $(function() { alert($('select').val()); })
Then you have events that you can attach to the Select element, for example, "change", "keyup", "keydown", etc. The usual event bindings are on “change” and “keyup”, since these are the 2 most common end events that take action in which the user expects a “change”. Read more about jQuery to learn more . Delegate () (deprecated version 1.6 and below),. on () ,. change () and . KeyUp () .
Example
$(document).on('change keyup', 'select', function(e) { var currentSelectVal = $(this).val(); alert(currentSelectVal); })
Now a delegating document change event is not "necessary", but it can really save a headache in the future. Delegation allows future elements (material not loaded into the DOM Load event) that match the Selector qualifications (exp. 'Select', '#elementID' or '.element-class') to automatically assign these event methods to them.
However, if you know that this will not be a problem, you can use event names like the jQuery Element Object Method with a little short code.
Example
$('select').change(function(e) { var currentSelectVal = $(this).val(); alert(currentSelectVal); })
The final note also contains “successful” and “complete” events that occur during some Ajax call. All jQuery Ajax methods have these 2 events anyway. These events allow you to perform actions after an Ajax call completes.
For example, if you want to get the value of the AFTER and Ajax selection fields, do the following:
Example
$.ajax({ url: 'http://www.mysite.com/ajax.php', succuess: function(data) { alert($("select#MyID").val()); } }) $.post("example.php", function() { alert("success"); }) .done(function() { alert($("select#MyID").val()); }) $("#element").load("example.php", function(response, status, xhr) { alert($("select#MyID").val()); });
Additional Information:
Something else you need to keep in mind, all jQuery Ajax methods (for example, .get, .post) are only shorthand versions of $.ajax({ /* options|callbacks */ }) !