3 Replies - 1546 Views - Last Post: 10 November 2011 - 04:28 AM

#1 rino4578  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 08-November 11

is it possible to use a single jquery to update multiple div tags

Posted 09 November 2011 - 10:40 PM

I have a webpage that contains a ton of php code. every so often i need a jquery to update a few areas of the page. I can make a seperate jquery for each div tag but it would be alot simpler if i could update each using the same jquery. This is my code for updating div tag predators.....
<!-- Only updates single div tag. look into updating multiple div tags in same jquery-->
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
 $(document).ready(function() {
 	 $("#predators").load("predators.php");
   var refreshId = setInterval(function() {
      $("#predators").load('predators.php?randval='+ Math.random());
   }, 9000);
   $.ajaxSetup({ cache: false });
});
</script>

The big question is; is it possible?

Is This A Good Question/Topic? 0
  • +

Replies To: is it possible to use a single jquery to update multiple div tags

#2 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 795
  • View blog
  • Posts: 1,681
  • Joined: 30-January 09

Re: is it possible to use a single jquery to update multiple div tags

Posted 09 November 2011 - 10:57 PM

I do this often, but I use separate AJAX calls. Otherwise how does the returned HTML get distributed amongst the targetted divs?

If all of the divs get the same (randomised parameter) return value, then you could use a class selector instead. I'm not sure if the Math.random function would fire for each though:
$(document).ready(function() {
  $(".predatorsClass").load("predators.php");
  var refreshId = setInterval(function() {
    $(this).load('predators.php?randval='+ Math.random());
  }, 9000);
  $.ajaxSetup({ cache: false });
});



EDIT: Actually, you could push your setInterval function into a separate function, and run a .each() over the selector. That would ensure that Math.random() fires on each call to the function. Nusy at work, otherwise I'd whip up the code and test it for you.

This post has been edited by e_i_pi: 09 November 2011 - 10:58 PM

Was This Post Helpful? 1
  • +
  • -

#3 rino4578  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 08-November 11

Re: is it possible to use a single jquery to update multiple div tags

Posted 09 November 2011 - 11:08 PM

Thanks I'll mess with it and see if I can make it happen.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,515
  • Joined: 23-August 08

Re: is it possible to use a single jquery to update multiple div tags

Posted 10 November 2011 - 04:28 AM

Moved to jQuery.

Some more detail about your returning data and the page on which you're putting it may be helpful. We may be able to come up with a combination of an updated PHP script and the calling Javascript to accomplish everything in one call.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1