Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I delete the previous values and add new values from Json with javascript?
    text
    copied!<p>I have this html code part</p> <pre><code> &lt;div id="prt"&gt; &lt;select name="selectPrt" class="span12" onchange="sta_callPRT();"&gt; &lt;option value="noPrt"&gt;Choose one vehicle.&lt;/option&gt; &lt;option value="prt1"&gt;PRT 1&lt;/option&gt; &lt;option value="prt2"&gt;PRT 2&lt;/option&gt; &lt;option value="prt3"&gt;PRT 3&lt;/option&gt; &lt;option value="prt4"&gt;PRT 4&lt;/option&gt; &lt;option value="prt5"&gt;PRT 5&lt;/option&gt; &lt;/select&gt; &lt;div class="row-fluid"&gt; &lt;div class="span6"&gt; &lt;div class="section"&gt; &lt;div class="sectionTitle"&gt; &lt;h3&gt;Vehicle Info&lt;/h3&gt; &lt;/div&gt; &lt;/div&gt; &lt;div id="state"&gt;&lt;strong&gt;State:&lt;/strong&gt; &lt;/div&gt; &lt;div id="freeSeats"&gt;&lt;strong&gt;freeSeats:&lt;/strong&gt;&lt;/div&gt; &lt;div id="battery"&gt;&lt;strong&gt;battery:&lt;/strong&gt;&lt;/div&gt; &lt;div id="totalDistance"&gt;&lt;strong&gt;totalDistance:&lt;/strong&gt;&lt;/div&gt; &lt;div id="location"&gt;&lt;strong&gt;Location:&lt;/strong&gt;&lt;/div&gt; &lt;div id="estimatedTime"&gt;&lt;strong&gt;estimatedTime"&lt;/strong&gt;&lt;/div&gt; &lt;div id="estimatedDistance"&gt;&lt;strong&gt;estimatedDistance:&lt;/strong&gt;&lt;/div&gt; &lt;div id="speed"&gt;&lt;strong&gt;speed:&lt;/strong&gt;&lt;/div&gt; </code></pre> <p>And this Javascript</p> <pre><code>function sta_callPRT(){ $.getJSON('PRTInfoGenerator.php', function(json){ $.each(json, function(key, value) { if(key=="state") { //$('#state').empty(); //document.getElementById("parameters").appendChild(divTag); $('#state').append(value); } if(key=="freeSeats") { $('#freeSeats').append(value); } if(key=="estimatedDistance"){ $('#estimatedDistance').append(value); } if(key=="estimatedTime"){ $('#estimatedTime').append(value); } if(key=="battery"){ $('#battery').append(value); } if(key=="speed"){ $('#speed').append(value); } if(key=="location"){ $.each(json.location, function(par_key, par_value) { $('#location').append(par_key+': '+par_value+' '); }); } if(key=="totalDistance"){ $('#totalDistance').append(value); } }); }); } </code></pre> <p>I want to write the vehicle values randomly in html.I have also php code to produce random values for vehicles.When I select a vehicle, it shows some values but in the second selection the new values are added to the end of the first.How can delete the previous values, and add the new values when new selection is made? For example this is first selection in the web page;</p> <pre><code>State: Running freeSeats:0 battery:95 totalDistance:8541 Location:x: -5 y: 34 estimatedTime:15 estimetedDistance:809 Speed:18 </code></pre> <p>This is second;</p> <pre><code>State: RunningRunning freeSeats:04 battery:9540 totalDistance:85411848 Location:x: -5 y: 34 x: 84 y: -70 estimatedTime:15269 estimetedDistance:809513 Speed:1818 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload