What are the steps to adjust text size using a slider?

I'm having trouble coding a feature that changes the text size dynamically with a slider, and it's not working as expected.

$('input').on('change', function() {
  var textSize = $(this).val();
  $('.userText p').css('font-size', textSize + 'em')
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="range" min="1.2" max="2.6" step=".2" id="slider" />
<div class="userText">
  <p>Some text that should dynamically change size</p>
</div>

Any advice or suggestions would be greatly appreciated!

Answer №1

Once the mouse button is released, the onchange event will be triggered. However, to have the event fire as you slide the slider, you should utilize the oninput event instead.

$('input').on('input', function() {
  var v = $(this).val();
  $('p').css('font-size', v + 'em')
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="range" min="1.2" max="2.6" step=".2" id="slider" />

<p>Some text that should dynamically change size</p>

Answer №2

Include the input event along with the change event, as the latter only triggers when the mouse is released. It's also helpful to add smaller step increments and a starting value equal to the minimum for improved user experience.

document.querySelector('input').addEventListener('input', e => document.querySelector('p').style.fontSize = e.target.value + 'em');
<input type="range" value="1.2" min="1.2" max="2.6" step=".0002" id="slider" />
<p>Some text that should dynamically change size.</p>

To explore variations in browser behavior, check out this discussion on onchange event behavior with input type=range not triggering in Firefox while dragging.

Using jQuery:

$('input').on('input', e => $('p').css('font-size', $(e.target).val() + 'em'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="range" value="1.2" min="1.2" max="2.6" step=".0002" id="slider" />
<p>Some text that should dynamically change size.</p>

Answer №3

Great job on your demo! Just a heads up, the new value is only applied after releasing the slider.
To update the value instantly, consider using the input event instead of the change event:

$('input').on('input', function() {
    var v = $(this).val();
    $('p').css('font-size', v + 'em')
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="range" min="1.2" max="2.6" step=".2" id="slider" />

<p>Some text that should dynamically change size</p>


The key difference is that input triggers when the user interface changes the value, while change fires when the element's state has been altered.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Div not centered after translation by 50% on the Y-axis

My HTML and body have a height of 100vh. Inside my body, I have a square that is 100px by 100px. When I apply top: 50% to the div, it moves down 50%. But when I apply translateY(50%) to the div, it does not move down 50%. Why is this? Please note that y ...

Concealing a div on submission and passing data to PHP

<?php echo $_POST['textvalue']; echo $_post['radiovalue']; ?> <div id="hidethis"> <form method="POST" action=""> <label>Tekst Value</label> <input type="text" name="textvalue"> <label>Radio Val ...

Data not displaying in selected option after selection

I'm having an issue retrieving the list of "states" once I choose a "country". The country list displays correctly, but upon selecting a country, the states do not appear in the dropdown selection. I am using ajax to send data to ajaxData.php from i ...

What is the best way to display a background image within my div element?

I'm trying to create my first child inside the div.missions, but it's not displaying. The only thing showing is the background color of the second child. Can someone assist me with this issue? Let me share my code with you: Here's my HTML: ...

My Javascript file is not being recognized by MVC

Initially, I created an MVC application without fully understanding that MVC is more backend-oriented than frontend-focused. I ended up building a simple website with just HTML, CSS, and Javascript files, which worked perfectly. However, when I tried to in ...

Retrieve the href attribute using jQuery when clicked

Is there a way to capture the href link using jQuery? For instance, consider the following: <li><a href="head.php">Head</a></li> <li><a href="body.php">Body</a></li> <li><a href="settings.php">S ...

Using data binding in VueJS to construct a dynamic URL

Exploring VueJS for the first time and it's been a great experience so far. However, I'm facing a couple of challenges with data binding. The requirement is for the customer to input an image URL, and no image should be displayed until a valid ...

Positioning the tiny rectangle containing the information icon within the larger horizontal rectangle

In the image attached, I am creating a horizontal rectangle.horizontal rectangle Using the following CSS, I am able to create a horizontal rectangle on my div: .Rectangle { width: 516px; height: 50px; border-radius: 4px; border: solid 1px rgba(78 ...

Can React components receive props or data when they are inserted into regular HTML code?

I have a project where I need to make updates to an old-fashioned website. The current layout is table-based and coded by hand. My idea to streamline the process is to use React to minimize repetitive coding tasks. Specifically, I want to loop through an a ...

clear background with logo embossed

I have encountered an issue while trying to place my logo on a black background with transparency. Instead of the background being transparent, now my logo is also becoming transparent which was not my intention. Does anyone have any suggestions or ideas ...

"Efficiently manage search suggestions and arrange outcomes for autofill text input utilizing jQuery

Is there a way to eliminate the message and structure the results in a sleek dropdown using CSS without relying on Bootstrap or pre-made CSS files? http://jsfiddle.net/SCuas/96/ var aCleanData = ['test','test1','abcd',' ...

Using Django, CSS, and Javascript, create a dynamic HTML form that shows or hides a text field based on the selection

How can I hide a text field in my Django form until a user selects a checkbox? I am a beginner in Django and web applications, so I don't know what to search for or where to start. Any guidance would be appreciated. Here is the solution I came up wi ...

"Utilizing jQuery AJAX to enable multiple file uploads with specific file extensions

I've been facing an issue with uploading multiple files using jQuery and AJAX. The problem arises when I attempt to submit a PNG or JPG file, but it fails to submit and instead alerts me to "Please Upload File" even though a file has been selected. ...

The phone number is not able to be clicked on

I have a phone number included in my markup that I would like to make clickable. Here is the code snippet: <h3 class="footer">Need assistance? Call <a href="tel:+180006XXXX">1800 06X XXX</a></h3> However, upon running this code, I ...

The inactive submit button due to an unfinished form is not functioning as expected

I am encountering an issue with a form that I have created to add a person to a list. The problem lies in the fact that there must be text entered into the input box in order for the "submit" button to function properly. However, this functionality is no ...

How can ngValue be leveraged with Angular Material's autocomplete feature?

I need to pass an object as my value and use [ngValue] instead of [value]. This is how my HTML is structured: <mat-input-container fxFlex="18" fxFlexOffset="1"> <input matInput placeholder="Example" [matAutocomplete]= ...

Error with HTML form validation

After clicking the "Send Request" button, the query string ?req_flag=0 is not appearing in the URL. What could be causing this issue? I want my URL to look like this: localhost/flavourr/send_req.php?req_flag=0&f_e_mail_add=value <pre> <form ...

The drop-down items on the navigation bar disappear too quickly

After spending hours getting everything laid out and functioning perfectly, suddenly the menus started disappearing when trying to select an item from the drop down. No matter what I tried, like changing ul ul{display: block;}, the issue persisted. It used ...

Trouble getting RMarkdown function to generate plots and convert to HTML

I am working on an R Markdown document that includes a function. This function requires a "case number" variable to filter some data frames, create a plot, and then output this plot to an HTML document using knit. The following is a high-level overview: -- ...

Load jQuery core asynchronously with a backup plan

Exploring performance optimization and non-blocking scripts in the header, my focus has been on asynchronously loading jQuery itself. In my search, I came across a jQuery Loader script that can async load jQuery and then handle and queue jQuery document r ...