javascript - How to call multiple different AJAX calls to the same div in HTML -


so reading data json file. able use ajax calls update div in html code. example:

$("#carsize").append(carinfo[0].carsize);

the div carsize data json file. there going 10 different cars many different properties such carsize. since carinfo array big , full of several different properties, there way simpler way of achieving without having create different div append each car's property.

html:

<html> <link href="json.css" rel="stylesheet" type="text/css">  <body>  <div class = "whitebar">     <div id = "box1">         <div id = "carprice"></div>         <div id = "carrate"></div>         <a href = "" id = "selbut">select</a>         <div id = "total"></div>     </div>  <div id = "box2">     <div id = "carsize"></div> </div>  </div>  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script src="my_script.js"></script> </body> </html> 

js

$(document).ready(function()  {     $.getscript("vehicle_data.json", function()      {         $("#carprice").append(carinfo[0].carprice + ".00");         $("#carrate").append(carinfo[0].carrate);         $("#total").append("total: " + carinfo[0].carprice * 7 + ".00 usd");         $("#carsize").append(carinfo[0].carsize);     });      }); 

json (sample)

var carinfo = [     {         carid: 1,         carprice : 35.00,         carrate : 'usd/day',         carsize : 'extra small car',         type : 'economy',     },     {         carid: 2,         carprice : 37.00,         carrate : 'usd/day',         carsize : 'small car',         type : 'compact',            } ]; 

note how js code 1 car , need done 9 other cars. ideas?

could like:

$(document).ready(function(){    $.getscript("vehicle_data.json", function() {      for(i=0; i<carinfo.length; i++){         var $carcontainer = $('<div class="carcontainer-'+i+'"></div>');         $carcontainer.append('<div id="carprice-'+i+'">'+carinfo[i].carprice + ".00"+'</div>')         .append('<div id="carrate-'+i+'">'+carinfo[i].carrate+'</div>')         .append('<div id="total-'+i+'">'+"total: " + carinfo[i].carprice * 7 + ".00 usd"+'</div>')         .append('<div id="carsize-'+i+'">'+carinfo[i].carsize+'</div>');         $('body').append($carcontainer);     }     });      }); 

Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -