javascript - JQuery unable to use find method using a variable -


i've been trying figure out why find method selector not detecting 'city' variable. here's code.

html

<div id="cities">   <div class="denver"></div>   <div class="seattle"></div> </div> 

jquery - data returned getcity.php string "denver, seattle".

$.post("getcity.php", function(data){     var cityarr= data.split(",");         $.each(cityarr, function(i, city){             $('#cities').find('.'+city).html(city);         });  }); 

this piece of code not work. if change find('.'+city) find('.denver') or find('.seattle') works.. doing wrong?

i think there whitespace after , in string use trim() or $.trim()

$('#cities').find('.' + city.trim()).html(city); 

or

$('#cities').find('.' + $.trim(city)).html(city); 

otherwise need split string comma space.

var cityarr= data.split(/\s*,\s*/);