javascript - Load items with ajax in div's -


i'm using following javascript load items in a few div's:

<script>     $.ajax({         url: 'load.php?type=diva',         cache: false,              success: function(data){         $('#diva').html(data);          }     });        $.ajax({         url: 'load.php?type=divb',         cache: false,              success: function(data){         $('#divb').html(data);          }     });  </script> 

html:

<div id="diva"> <!-- load diva item here --> </div> <div id="divb"> <!-- load divb item here --> </div> 

i'm looking (and think) there's better way this. similar this: 1 function div's, automatically loads items in (multiply) div's:

function loaditem(<div id>) {  $.ajax({             url: 'load.php?type=<div id>',             cache: false,                  success: function(data){             $('<div id>').html(data);              }         });  }; 

i think better:

function loaditem(div) {    $.ajax({       url: 'load.php?type='+div,       cache: false,            success: function(data){          $('#'+div).html(data);        }    });  };  $(function(){    $('div[id^="div"]').each(function(){       loaditem(this.id);    }); }); 

what code doing:

  1. make global function putting function outside of doc ready.
  2. in doc ready block loop through div id starts div , pass id function.
  3. so if there 2 divs there 2 ajax calls.

this late reply:

$('div[class*="div"]').each(function(){ // class contains div    loaditem(this.classname.split(' ')[1]);  });// split ' ' space , pass second index arrays 0 based index. 

Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -