Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

85
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!