Represent data from json file in rating stars without jquery!! pure javaScript -


i trying load data json file using javascript , need represent hotel2show.rating in form of stars, represent them dependig on value 'hotels.json'

here javascript

function gethotels(i){  var xhr = new xmlhttprequest(); xhr.onreadystatechange = function() {     if (xhr.readystate === xmlhttprequest.done) {         if (xhr.status === 200) {                 hotel=json.parse(xhr.responsetext);                 var hotel2show = hotel.hotels[i];                 document.getelementbyid("img-container").innerhtml =                  "<img src='"+hotel2show.imgurl+"'>"+          "<p id='name'><strong>"+ hotel2show.name +"</strong></p>" +"<br/>" + "<p id='rating'><strong>"+ hotel2show.rating +"</strong></p>" +"<br/>" + "<br/>" +"<p id='price'><strong>"+ '&pound;' +hotel2show.price +              "</strong></p>" + "<p id='text'><strong>"+ 'total hotel stay' +"</strong></p>";          } else {                 alert("ha existido un error con el servidor");          }     }  }; xhr.open("get",'hotels.json', true); xhr.send(); 

and here html

<div class="container">  <div id="lista">   <ul>     <button onclick="gethotels(0)">hotel sunny palms</button>    <button onclick="gethotels(1)">hotel snowy mountains</button>    <button onclick="gethotels(2)">hotel windy sails</button>    <button onclick="gethotels(3)">hotel middle of nowhere</button>   </ul> </div> <div class="banner-section" id="img-container"> </div> 

and hotels.json

"hotels": [     {         "name": "hotel sunny palms",         "imgurl": "imgs/sunny.jpg",         "rating": 5,         "price": 108.00     },     {         "name": "hotel snowy mountains",         "imgurl": "imgs/snowy.jpg",         "rating": 4,         "price": 120.00     },     {         "name": "hotel windy sails",         "imgurl": "imgs/windy.jpg",         "rating": 3,         "price": 110.00     },     {         "name": "hotel middle of nowhere",         "imgurl": "imgs/nowhere.jpg",         "rating": 4,         "price": 199.00     } ] 

any appreciated

i assume know how parse out ratings, right? if displaying single star values (whole numbers), can write out class onto span element style css change background image be.

so, make show 1-5 stars 4 different images.

it's solution; not cleanest or scalable, works situation.

so first off nightmare.

let's clean bit?

var appendstring = []; appendstring[0] =  "<img src='"+hotel2show.imgurl+"'>"; appendstring[1] = "<p id='name'><strong>"+ hotel2show.name +"</strong></p><br/>"; switch(hotel2show.rating): case(1): appendstring[2] = "<p id='rating' class='rating-1'><strong>"; break; case(2): appendstirng[2] = "<p id='rating' class='rating-2><strong>"; break; //etc   appendstring[3] = hotel2show.rating +"</strong></p>"; appendstring[4] = "<br/><br/>"; appendstring[5] = "<p id='price'><strong>'&pound;'" + hotel2show.price + "</strong></p>"; appendstring[6] = "<p id='text'><strong>"+ 'total hotel stay' +"</strong></p>"; document.getelementbyid("img-container").innerhtml = appendstring.join(' '); 

note: switch statement syntax may incorrect.


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -