Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

87
Visualizações
Custom font javascscript

So I have some code to change my font, but I also want an option to input a custom font:
Javascript:

changeFont = function(){
    var select = document.getElementById('font');
    var fontValue = select.options[select.selectedIndex].value;
    console.log(value);
    if (fontValue === "custom"){
        var custom = document.getElementById("font");
        custom.style.visibility='visible'
    }
    else{
        document.documentElement.style
        .setProperty('--font', fontValue);
    }
    
    
}

Html:
 <select id="font" style="margin-left:10px; margin-right: 10px;" onchange="changeFont()">
        <option value="Fira Code">Defualt</option>
        <option value="sans-serif">Sans Serif</option>
        <option value="serif">Serif</option>
        <option value="custom">Custom</option>


        
      
    </select> 
    <h5 class="customFont">Custom Font</h5>
<input class="customFont" id="customFont">

But unfortuantley it doesnt work, is my if statment wrong or something, im kinda stuck rn. Thanks

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I guess you need a select to get the pre-defined font value and an input to get the customized font value. If you want it, you can try following code:

changeFont = function() {
  var select = document.getElementById('font');
  var fontValue = select.options[select.selectedIndex].value;

  if (fontValue === "custom") {
    var custom = document.getElementById("customFont");

    custom.style.visibility = 'visible'
  } else {
    document.documentElement.style.setProperty('font-family', fontValue);
  }
}

inputFont = function() {
  var custom = document.getElementById("customFont");

  document.documentElement.style.setProperty('font-family', custom.value);
}
<div id="main">
  <select id="font" style="margin-left:10px; margin-right: 10px;" onchange="changeFont()">
    <option value="Fira Code">Defualt</option>
    <option value="sans-serif">Sans Serif</option>
    <option value="serif">Serif</option>
    <option value="custom">Custom</option>
  </select>

  <h5 class="customFont">Custom Font</h5>
  <input class="customFont" id="customFont" onchange="inputFont()">
</div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda