tags:

views:

54

answers:

3

Hi,

I have a first.js file included in the page index.php that have something like this:

$(function(){

    $("#my_slider").slider("value", 10);

});

And them in index.php I have some dynamicly created slidders:

<?php function slidders($config, $addon)
{
    $return = '
    <script type="text/javascript">
        $(function() {
            $("#slider_'.$addon['p_cod'].'").slider({
            min: '.$config['min'].',
            max: '.$config['max'].',
            step: '.$config['step'].',
            slide: function(event, ui) {
                $("#cod_'.$addon['p_cod'].'").val(ui.value);
                $(".cod_'.$addon['p_cod'].'").html(ui.value+"'.@$unit.'");
            },
            change: function(event, ui) { 
                $("#cod_'.$addon['p_cod'].'").change();
            }
        });
            $("#cod_'.$addon['p_cod'].'").val($("#slider_'.$addon['p_cod'].'").slider("value"));
            $(".cod_'.$addon['p_cod'].'").html($("#slider_'.$addon['p_cod'].'").slider("value")+"'.@$unit.'");
    });
    </script>';
    return $return;
} ?>

The problem is, because my index.php sliders are being instantiated after my first.js I can't set up a value there, is there any event like "after all $(document).ready() have run" that I can use in first.js to manipulate the sliders created in index.php?

+2  A: 

One of the goals of jQuery's $(document).ready() function is that the standard javascript window.onload event won't run until everything on the page is loaded (including images, etc.), even though functions can usefully start running well before that. So $(document).ready() runs as soon as the DOM hierarchy is constructed.

Given this, one option would be to run your "after everything else" script on page load. This won't guarantee that all of the $(document).ready() scripts have completed, though.

A better but more complicated option is to create a function that checks for the existence of the sliders, and if they don't exist, calls "setTimeout" on itself, so it can wait a few milliseconds and try again.

(To answer your specific question, no, there is no "after all $(document).ready() have run" callback.)

JacobM
+2  A: 

Dont know how to tell when the last $(document).ready() function fired, but $(window).load() function fires after the $(document).ready()

Here is a good link for explination

http://blog.eizesus.com/2009/03/jquery-s-ready-runs-before-load-event/

John Hartsock
Thanks John that was the solution I just changed my $(function(){("#my_slider").slider("value", 10);}); for $(window).load(function(){("#my_slider").slider("value", 10);}); and it worked. Cheers brother!
mjsilva
@mjsilva...Glad I could help
John Hartsock
A: 

You would be better to add a unique class to your sliders rather than the #my_slider since that will only fire for the element with id = my_slider.

If you want to have the same functionality you should check out .live from jquery it will put the same functionality onto elements that are added after the binding.

If that will not work for you then when you add the new sliders you will have to go through them in the DOM and call the necessary function.

darren102
Hi darren102, my sliders have unique Ids, the codded that I posted does not reelect my running code, I just post it has it is as an example of what I was trying to accomplish. Sorry if that causes som confusion, I'm still a newb in asking questions @ stackoverflow :P
mjsilva