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
5 Answers
$(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();
});

- 132,184
- 23
- 144
- 116
-
The last variant can also be used like this: $(function() { yourCodeHere(); }); – Mathias Bynens May 20 '09 at 21:05
-
Sorry one more issue. I have the function outside of the document.ready and it is called from a click (By clicking the radio button option) is there a way to just call (Simulate) the click event on the body load? – Phill Pafford May 20 '09 at 21:11
-
@Phill, sure, you can use $('#button').click(). I edited my answer. – Ayman Hourieh May 20 '09 at 21:28
-
@Phil - Just to be clear on what Ayman is saying, the full answer is: $(function() { $('#button').click(); }); – KyleFarris May 20 '09 at 21:47
-
Thanks all but I think you miss understood me. The event is happening now with the click() but I want to also have it filter the options from the ready() as well without the click() event. Please see the post link above for the code and thanks again :) – Phill Pafford May 21 '09 at 14:10
Crude, but does what you want, breaks the execution scope:
$(function(){
setTimeout(function(){
//Code to call
},1);
});

- 61
- 1
-
8**Bad answer.** This is worse than crude. It may work, or it may completely fail - and worse, the success (or not) is dictated by factors completely outside the developer's control, and *very* likely beyond the user's understanding. – Winfield Trail Feb 12 '14 at 04:27
In regards to the question in your comment:
Assuming that you've previously bound your function to the click event of the radio button, add this to your $(document).ready
function:
$('#[radioButtonOptionID]').click()
Without a parameter, that simulates the click event.
-
This didn't work either, any other thoughts? Could you check the code from the link above? Thanks – Phill Pafford May 21 '09 at 14:10