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:
- make global function putting function outside of doc ready.
- in doc ready block loop through div id starts
div
, pass id function. - 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
Post a Comment