Phill Pafford Phill Pafford - 5 months ago 27
jQuery Question

jQuery call function after load

Need to call a filter function on some options based on a radio selected (Link here), How can I call this after the page is loaded? the radio is set from a DB call and I would like to filter the options

Answer
$(document).ready(my_function);

Or

$(document).ready(function () {
  // Function code here.
});

Or the shorter but less readable variant:

$(my_function);

All of these will cause my_function to be called after the DOM loads.

See the ready event documentation for more details.

Binds a function to be executed whenever the DOM is ready to be traversed and manipulated.

Edit:

To simulate a click, use the click() method without arguments:

$('#button').click();

From the docs:

Triggers the click event of each matched element. Causes all of the functions that have been bound to that click event to be executed.

To put it all together, the following code simulates a click when the document finishes loading:

$(function () {
  $('#button').click();
});