jquery - How to avoid from multiple times data being appended to the select tag -


on ajax request ,i getting response server

{     "screen": [         {             "screen_name": "scr1",             "screen_id": "1"         },         {             "screen_name": "scr2",             "screen_id": "2"         },         {             "screen_name": "scr3",             "screen_id": "3"         },         {             "screen_name": "scr4",             "screen_id": "4"         },         {             "screen_name": "scr5",             "screen_id": "5"         },         {             "screen_name": "bigscr",             "screen_id": "6"         }     ] } 

the problem facing everytime click on drop down , data being appended multiple times select html named scname

i appending data select tag , screen looks way

enter image description here

this code

$(document).on("click", "#scname", function() {      var htmloptions = '';            $.ajax({         type: 'get',         url: url+'/oms/oms1/getscreens?location_id='+location_id,         jsonpcallback: 'jsoncallback',         datatype: 'jsonp',         jsonp: false,         success: function(response) {             var jsondata = json.stringify(response);             jsondata = json.parse(jsondata);         (var = 0; < jsondata.screen.length; i++) {         var screenname = jsondata.screen[i].screen_name.trim();         var screenid = jsondata.screen[i].screen_id.trim();         htmloptions+='<option value="'+screenid+'">'+screenname+'</option>';      }      $('#scname').append(htmloptions);          },         error: function(e) {              alert('into error');          }     });  });                   <select name="" id="scname">                     <option> write screen name</option>                 </select> 

even though clearing htmloptipns every time , still data being appended multiple times , please let me know how avoid ??

you appending data $('#scname') each time click on select, without resetting it. clearing htmloptions not enough since it's local variable. reset content of $('#scname') before appending new values.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -