5

I am trying to run this code but every time I open my webpage it doesn't load and I don't get the text I echo with PHP in the file "money.php". But if I open the console in my browser and run the code it does load. How can I fix this?

<script type="text/javascript">
$('[data-update]').each(function() {
  var self = $(this);
  var target = self.data('update');   
  var refreshId =  setInterval(function() { self.load(target); }, self.data('refresh-interval'));
});
</script>
<div data-update="money.php" data-refresh-interval="500">
halfer
  • 19,824
  • 17
  • 99
  • 186
Patric Nøis
  • 208
  • 2
  • 8
  • 27

3 Answers3

3

Use This

    <script type="text/javascript">
        $(function() {
            $('[data-update]').each(function () {
                var self = $(this);
                var target = self.data('update');
                var refreshId = setInterval(function () {
                    self.load(target);
                }, self.data('refresh-interval'));
            });
        });
    </script>

OR This

    <script type="text/javascript">
        $(document).ready(function() {
            $('[data-update]').each(function () {
                var self = $(this);
                var target = self.data('update');
                var refreshId = setInterval(function () {
                    self.load(target);
                }, self.data('refresh-interval'));
            });
        });
    </script>
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
1

Wrap your code in $(function(){ ... }); so it runs after the DOM is ready:

<script type="text/javascript">
$(function(){
  $('[data-update]').each(function() {
    var self = $(this);
    var target = self.data('update');   
    var refreshId =  setInterval(function() { self.load(target); }, self.data('refresh-interval'));
  });
});
</script>

More info: http://api.jquery.com/ready/

Ryan Jones
  • 161
  • 4
1

Run the javascript/JQuery after

<div data-update="money.php" data-refresh-interval="500">