I am making a select box with all the fonts in google fonts API. I have referred this link to learn more about API but till now i was not able to make it.
I am adding this Fiddle which i made for this.
HTML
<select id="styleFont">
<option value="0">Myraid Pro</option>
<option value="1">Sans ref</option>
<option value="2">Times New Roman</option>
<option value="3"> Arial</option>
</select>
<br>
<textarea id="custom_text"></textarea>
CSS
#custom_text{ resize: none;}
Script
$("#styleFont").change(function () {
var id =$('#styleFont option' + ':selected').text();
$("#custom_text").css('font-family',id);
});
My API key is
How can i link those fonts to my select box in the fiddle?
I am making a select box with all the fonts in google fonts API. I have referred this https://developers.google./webfonts/docs/developer_api link to learn more about API but till now i was not able to make it.
I am adding this Fiddle which i made for this.
HTML
<select id="styleFont">
<option value="0">Myraid Pro</option>
<option value="1">Sans ref</option>
<option value="2">Times New Roman</option>
<option value="3"> Arial</option>
</select>
<br>
<textarea id="custom_text"></textarea>
CSS
#custom_text{ resize: none;}
Script
$("#styleFont").change(function () {
var id =$('#styleFont option' + ':selected').text();
$("#custom_text").css('font-family',id);
});
My API key is https://www.googleapis./webfonts/v1/webfonts?key=AIzaSyB8Ua6XIfe-gqbkE8P3XL4spd0x8Ft7eWo
How can i link those fonts to my select box in the fiddle?
Share Improve this question edited Dec 7, 2012 at 5:55 Vivek Dragon asked Dec 7, 2012 at 5:01 Vivek DragonVivek Dragon 2,2485 gold badges28 silver badges48 bronze badges 3- What exactly are you trying to do? – trebuchet Commented Dec 7, 2012 at 5:52
- @trebuchet i am trying to add the list of Google font API in my select . I have gained my API key which is googleapis./webfonts/v1/… now i need to connect my select with it – Vivek Dragon Commented Dec 7, 2012 at 5:55
- @trebuchet it worked fine in jsbin But when i copy all the code to my html page its not working what is the problem in that or what i have to add – Vivek Dragon Commented Dec 7, 2012 at 11:26
2 Answers
Reset to default 6This is an old question, I didn't find anything as good for this as I wanted, so I developed my own selector:
Github https://github./maPer77/Select2-Google-Fonts
Demonstration: https://maper77.github.io/Select2-Google-Fonts/
See it working:
selectGfont({
key: 'AIzaSyDlD2NdRw4MDt-jDoTE_Hz3JqNpl154_qo',
containerFonte: '#selectGFont',
containerVariante: '#selectGFontVariante',
sort: 'popularity',
onSelectFonte: 'sGFselecionado'
});
sGFselecionado = function(fonte, variante, json){
console.log( 'fonte', fonte );
console.log( 'variante', variante );
console.log( 'json', json );
};
<link href="https://stackpath.bootstrapcdn./bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare./ajax/libs/select2/4.0.7/css/select2.min.css" rel="stylesheet">
<link href="https://select2.github.io/select2-bootstrap-theme/css/select2-bootstrap.css" rel="stylesheet">
<link href="https://maper77.github.io/Select2-Google-Fonts/src/selectGfont.min.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn./bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/select2/4.0.7/js/select2.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/webfont/1.6.28/webfontloader.js"></script>
<script src="https://maper77.github.io/Select2-Google-Fonts/src/selectGfont.min.js"></script>
<div class="row justify-content-center">
<!-- Fonte GOOGLE -->
<div class="col-md-12 col-lg-10 col-xl-8">
<label><span class="selectGFontTotal"></span> Google Fonts</label>
<div class="input-group input-group-lg">
<select id='selectGFont' data-default='Play' class="form-control invisible"></select>
<div class="input-group-append">
<select id='selectGFontVariante' data-default='regular' class="form-control invisible"></select>
</div>
</div>
</div>
</div>
Just use jquery to get the font list, then add each font to your select:
$.getJSON("https://www.googleapis./webfonts/v1/webfonts?key=AIzaSyB8Ua6XIfe-gqbkE8P3XL4spd0x8Ft7eWo", function(fonts){
for (var i = 0; i < fonts.items.length; i++) {
$('#styleFont')
.append($("<option></option>")
.attr("value", fonts.items[i].family)
.text(fonts.items[i].family));
}
});
EDIT: This uses JSONP
function SetFonts(fonts) {
for (var i = 0; i < fonts.items.length; i++) {
$('#styleFont')
.append($("<option></option>")
.attr("value", fonts.items[i].family)
.text(fonts.items[i].family));
}
}
var script = document.createElement('script');
script.src = 'https://www.googleapis./webfonts/v1/webfonts?key=AIzaSyB8Ua6XIfe-gqbkE8P3XL4spd0x8Ft7eWo&callback=SetFonts';
document.body.appendChild(script);