I apply each loop in jQuery with onchange()
event.
JavaScript
x
23
23
1
$(function() {
2
$('#datatype').change(function(){
3
$.getJSON('/background_process', {
4
}, function(data) {
5
var toAppend = '';
6
$.each(data.result,function(i,o){
7
toAppend += '<option >'+o+'</option>';
8
});
9
$("#result").append(toAppend);
10
});
11
return false;
12
});
13
});
14
15
<select name="datatype" id='datatype'>
16
<option selected disabled >SELECT DATATYPE</option>
17
<option value="RAW">RAW</option>
18
<option value="DEPTH">DEPTH</option>
19
</select>
20
<select id=result name="result">
21
<option value="NULL">SELECT TOKEN</option>
22
</select>
23
So my problem is when I select option(“RAW”) it return result as option values and then I select “DEPTH” option and it add “RAW” and “DEPTH” return result and give both data as option.
output – when I select “RAW”
JavaScript
1
3
1
<option>"raw-result"<option>
2
<option>"raw-result"<option>
3
When I select “DEPTH”
JavaScript
1
5
1
<option>"raw-data"<option>
2
<option>"raw-data"<option>
3
<option>"depth-data"<option>
4
<option>"depth-data"<option>
5
So I want, when I select option it return loop data and when again select option then it return new loop data not append data with previous select.
Output should like this- When I select “DEPTH” option after “RAW” option so it return
JavaScript
1
3
1
<option>"depth-data"<option>
2
<option>"depth-data"<option>
3
But it does not return that result.
Advertisement
Answer
You are appending the new data instead of replacing old one with it.
Instead of append
use html
JavaScript
1
2
1
$("#result").html(toAppend);
2