cannondale;11037459 wrote:
AJAX but can only conclude that there may bee some truth to the claim that <select> tag can not receive a .load event request.
I missed this in my initial reply, but as you point out, there can be no load event for the select element. However the reason for that is html, not jquery. Apart from the body element, only elements that actually load their content in separate requests can have load event handlers, such as images, script elements etc. This renders my initial argument about in which order things happen irrelevant
cannondale;11037459 wrote:
1) If the element id "dd_category_search" did not exist, I believe I would see a console error when the load tried to fire.
I'd expect nothing to happen rather than getting an error. But try it out to be certain.
My reasoning is that if you use jQuery('#id') before the DOM is ready, or if you have no such id in the document, the jQuery constructor would return an object which contains a list of nodes of length 0 - because no nodes where found. Then you would attach event handlers to all those (zero) objects. This is perfectly legal, albeit pointless.
cannondale;11037459 wrote:
2) My understanding of
$( document ).ready()
is that code included inside $( document ).ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute
Sort of. Javascript code can execute as soon as it is parsed. But it cannot find DOM nodes that do not exist and it cannot manipulate the DOM tree until the DOM tree has been constructed. Document ready fires when the DOM tree has been constructed.
cannondale;11037459 wrote:
3) The code
request.always(function() {
alert('Successfully populated search sub-cat'); //for testing only
});
does not execute.
Yes it does - or yes it would. But only when it should, and this "always" is in relation to when the ajax requests are made. And only if you do not have syntax errors (such as superfluous quotes somewhere). It will execute on every ajax request. In case we are talking about the ajax request for the load event handler which never gets executed, always will still fire every time that gets executed. It just happens to be 0 times.
With some additional testing I added the following separate code block,
$(document).ready(function(){;
$(window).load(function(){;
The double use of these is pointless. The first line attaches an event handler for when the document DOM is ready. When it is and the code is executed, it will attach an event handler for when the entire document has finished loading.
If you want your code to execute when the document has finished loading, attach no ready handler, only the load handler. But in this case, I'd say remove the load handler instead.
The rest of the code does the exact same thing you want to do when the element changes value. That is, when the change event fires. And that is why I suggested that you trigger the change event. Never write the same code in multiple places. Thus
$(document).ready(function(){
// attach change event handler to element
$('#id').on('change', function() { alert('stuff'); });
// trigger the change event for this element
$('#id').trigger('change');
// but since it's bad form (inefficient) to repeatedly call the jquery constructor for the same element, either
$('#id').on('change', function() { alert('stuff'); }).trigger('change');
// Or the possibly more readable
$el = $('#id');
$el.on('change', function() { alert('stuff'); });
$el.trigger('change');