I have a returned JSON string which I have parsed and put through into an array and then formatted to put into a select dropdown list:
function populateGroups(){
var categories = '';
for( category in gCategories ){
categories += '<option name="' + gCategories[category] + '">' + gCategories[category] + '</option>';
categories.replace(' ', '_');
}
$('select[name="category"]').html(categories);
}
The problem I have is that the option names (which are the same as the values) contain spaces which I need to replace with something that would be valid, such as an underscore ‘_’.
That is why I have tried doing the categories.replace(‘ ‘, ‘_’); I have also tried doing this with regex, although my RegEx is not very good 😛
Any ideas on how this would be done?
Thanks!
[Note]
Here is the JSFiddle : http://jsfiddle.net/pKYr4/
You should update the string as well;
…a problem though is that not just the values are formated/trimmed as
<option name="...will be affected as well.This can be solved this way;