Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change font-size When Selection is made

I need to change the font-size in a div when someone selects a different font size.

Here is my code which I know is working because I added an alert to it. The only thing not working is it changing the font size.

CSS:

.heading {
font-size:18px;
 }

JAVASCRIPT:

$('#htsize').on('change', function (e) {
var optionSelected = $("option:selected", this);
var valueSelected = this.value;
$(".heading").css("font-size", valueSelected);
});

HTML:

<SELECT id="htsize" name="htsize" style="width:100px; font-size:10px;">
<option value=""> SELECT ONE </option>
<option value="12">12px</option>
<option value="13">13px</option>
<option value="14">14px</option>
<option value="15">15px</option>
<option value="16">16px</option>
<option value="17">17px</option>
<option value="18">18px</option>
<option value="19">19px</option>
<option value="20">20px</option>
<option value="21">21px</option>
<option value="22">22px</option>
<option value="23">23px</option>
<option value="24">24px</option>
<option value="25">25px</option>
<option value="26">26px</option>
<option value="27">27px</option>
<option value="28">28px</option>
<option value="29">29px</option>
<option value="30">30px</option>
</select>

<div id="headline2" class="heading" style="position: relative; zindex: 99; width: 220px; padding: 5px; color: #ffffff; text-align: center;"></div>

I just can't figure out what is wrong.

like image 676
Randy Thomas Avatar asked May 24 '17 20:05

Randy Thomas


People also ask

How do you change font size when highlighting?

Select the text or cells with text you want to change. To select all text in a Word document, press Ctrl + A. On the Home tab, click the font size in the Font Size box.

How can you increase the selected font size by pressing?

Keyboard shortcutHold down the Ctrl and press the + to increase the font size or - to decrease the font size. Pressing either of these keys while continuing to hold down the control key continues to increase or decrease the font until it reaches its maximum.


2 Answers

You need to add px to the every option value or valueSelected+'px'

like image 79
Bohdan Didukh Avatar answered Sep 23 '22 19:09

Bohdan Didukh


You are missing the "px" from your value. So you can add it to the values of your dropdown or append it in your script like so:

$('#htsize').on('change', function(e) {
  var optionSelected = $("option:selected", this);
  var valueSelected = this.value + "px";
  $(".heading").css("font-size", valueSelected);
});
.heading {
  font-size: 18px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<SELECT id="htsize" name="htsize" style="width:100px; font-size:10px;">
<option value=""> SELECT ONE </option>
<option value="12">12px</option>
<option value="13">13px</option>
<option value="14">14px</option>
<option value="15">15px</option>
<option value="16">16px</option>
<option value="17">17px</option>
<option value="18">18px</option>
<option value="19">19px</option>
<option value="20">20px</option>
<option value="21">21px</option>
<option value="22">22px</option>
<option value="23">23px</option>
<option value="24">24px</option>
<option value="25">25px</option>
<option value="26">26px</option>
<option value="27">27px</option>
<option value="28">28px</option>
<option value="29">29px</option>
<option value="30">30px</option>
</select>

<div id="headline2" class="heading" style="">My Headline</div>
like image 21
CodeLikeBeaker Avatar answered Sep 21 '22 19:09

CodeLikeBeaker