tags:

views:

2034

answers:

2

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  A: 
$(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();
});
Ayman Hourieh
The last variant can also be used like this: $(function() { yourCodeHere(); });
Mathias Bynens
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
@Phill, sure, you can use $('#button').click(). I edited my answer.
Ayman Hourieh
@Phil - Just to be clear on what Ayman is saying, the full answer is: $(function() { $('#button').click(); });
KyleFarris
@KyleFarris - I updated the answer with that. Thanks!
Ayman Hourieh
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
A: 

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.

Ender
This didn't work either, any other thoughts? Could you check the code from the link above? Thanks
Phill Pafford