Jacob G
Jacob G

Reputation: 14152

How do you change the font-family of a input element and text with JavaScript?

I have a problem with JavaScript that i wrote to change the font in a <input type="text"> element and a <p> element.

function tnrchan() {
  document.getElementsByClassName("pedfields").style.fontFamily = "Times New Roman";
  document.getElementById("preview").style.fontFamily = "Times New Roman";
}

function bschan() {
  document.getElementsByClassName("pedfields").style.fontFamily = "Brush Script MT";
  document.getElementById("preview").style.fontFamily = "Brush Script MT";
}
<fieldset>
  <legend>Pick Your Font</legend>
  <table>
    <tr>
      <th id="tms">Times New Roman</th>
      <th><input type="radio" id="tmsr" name="font" onclick="tnrchan()" checked="checked"></th>
    </tr>
    <tr>
      <th id="bs">Brush Script</th>
      <th><input type="radio" id="bsr" onclick="bschan()" name="font"></th>
      <th>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;More Fonts Coming Soon!</th>
    </tr>
    <tr>
      <th id="al">Arial</th>
      <th><input type="radio" id="alr" name="font"></th>
    </tr>
    <th id="fs">French Script MT</th>
    <th><input type="radio" id="fsr" name="font"></th>
  </table>
</fieldset>
<fieldset>
  <legend>Preview</legend>
  <p id="preview">This Is What Your Words Will Look Like!</p>
  <br>
  <label>Try It Out!<br><input type="text" class="pedfields"placeholder="EXAMPLE..."></label>
</fieldset>

I want the font-family of the <input> and <p> to change to one of the fonts when the functions are called.

Anyone have an idea on what I am doing wrong?

EDIT I got it working. It needed the following code:

function changeFont(fontName) {

  var list = document.getElementsByClassName("preview");

  for (i = 0; i < list.length; ++i) {
    list[i].style.fontFamily = fontName;
  }
}
<fieldset>
  <legend>Pick Your Font</legend>
  <table>
    <tr>
      <th id="tms">Times New Roman</th>
      <th><input type="radio" id="tmsr" name="font" onclick="changeFont('Times New Roman')" checked="checked"></th>
    </tr>
    <tr>
      <th id="bs">Brush Script</th>
      <th><input type="radio" id="bsr" onclick="changeFont('Brush Script MT')" name="font"></th>
      <th>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;More Fonts Coming Soon!</th>
    </tr>
    <tr>
      <th id="al">Arial</th>
      <th><input type="radio" id="alr" onclick="changeFont('Arial')" name="font"></th>
    </tr>
    <th id="fs">French Script MT</th>
    <th><input type="radio" id="fsr" onclick="changeFont('French Script MT')" name="font"></th>
  </table>
</fieldset>
<fieldset>
  <legend>Preview</legend>
  <p id="preview" class="preview">This Is What Your Words Will Look Like!</p>
  <br>
  <label>Try It Out!<br><input type="text" class="preview" placeholder="EXAMPLE..."></label>
</fieldset>

Upvotes: 2

Views: 3831

Answers (2)

williambq
williambq

Reputation: 1125

Typically, any hyphenated property in CSS is going to be a camelCased property in JavaScript. Thus, you would use style.fontFamily.

However, I would recommend switching/applying the class of the element and letting the CSS control the details.

Upvotes: 1

Quentin
Quentin

Reputation: 943108

document.getElementByClassName("pedfields")

It says elements, with an s, plural.

That returns a NodeList, not an Element. You have to loop over it like an array.

.style.font-family

Identifiers cannot contain - characters. They are subtraction operators. You need to switch to camelCase: fontFamily.

Upvotes: 5

Related Questions