Is it possible to adjust the range of a range slider based on the selection of a radio button or other input method?

I have a query and I’m hopeful you can assist:

 function UpdateTemperature() {
  var selectedGrade = $( "input[name='radios']:checked" ).val(); 
  $( ".c_f" ).text(selectedGrade); 
  var value1 = $("#tempMin").val();
  var value2 = $("#tempMax").val();
  $( "#minT" ).text(value1);
  $( "#maxT" ).text(value2);
}
$("input[name='radios']").change( UpdateTemperature );
$("#tempMin").change( UpdateTemperature );
$("#tempMax").change( UpdateTemperature );
UpdateTemperature();

$("#radio1").click( function() {
//
});

$("#radio2").click( function() {
//
});
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/css/materialize.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js"></script>

<span class="temp">Min</span><span id="minT" class="min"></span><span class="c_f"></span><span class="max">Max</span><span id="maxT" class="max"></span><span class="c_f"></span>

  <div class="row">
        <div class="input-radio col s12 m12 offset-m5 offset-s5" style="margin-bottom: 0;">
                <input type="radio" id="radio1" value="C" name="radios" checked>
                <label clase ="radioLabel" for="radio1">C</label>
                <br>
                <input type="radio" id="radio2" name="radios" value="F">
                <label clase ="radioLabel" for="radio2">F</label>    
        </div>
      </div>

  <div class="row" id="inline2">
    <div class="col s12 m12 l12 offset-m3 offset-l4 temp_uv">
        <div class="quantity">
        <input type="number" id="tempMin" name="temp_min" step="1" min="-60" max="60" placeholder="-5 C" value="-5" />
        </div>
        <div class="line"><strong>—</strong></div>
        <div class="quantity1">
        <input type="number" id="tempMax" name="temp_max" step="1" min="-60" max="60" placeholder="40 C" value="40" />
        </div>
    </div>
  </div>
<br>
  <div class="row">
      <div id="temp_Slider_C"></div>
      
  </div>

The code above contains two radio input types and two number input fields. The radio inputs allow the selection of temperature unit: °C or °F, while the number inputs are used to enter minimum and maximum temperatures. I am looking to convert values from °C to °F or vice versa by clicking on the radio input options. For instance, if the temperature is in °C and I click on the F value radio input, the temperature values should change accordingly in the number input fields... Do you have any suggestions on how I can achieve this using JS/jQuery?

Note: C = (F - 32) * 5 / 9 (Celsius)

F = ((C * 9) / 5 ) + 32 (Fahrenheit)

Answer №1

Use just one change() function to handle both radio inputs like this:

$('[name="radios"]').on('change',function() {
  // get the value for C or F
  var ThisVal = $(this).val().toLowerCase();
  // input values
  var value1 = $("#tempMin").val();
  var value2 = $("#tempMax").val();
  if(ThisVal == 'c'){
    $("#tempMin").val((value1 - 32) * 5 / 9);
    $("#tempMax").val((value2 - 32) * 5 / 9);
  }else{
    $("#tempMin").val(((value1 * 9) / 5 ) + 32);
    $("#tempMax").val(((value2 * 9) / 5 ) + 32);
  }
  GetGradeTemp ();
});

Check out the complete code below:

function GetGradeTemp () {
  var grades = $( "input[name='radios']:checked" ).val(); 
  $( ".c_f" ).text(grades); 
  var value1 = $("#tempMin").val();
  var value2 = $("#tempMax").val();
  $( "#minT" ).text(value1);
  $( "#maxT" ).text(value2);
}
$("input[name='radios']").change( GetGradeTemp );
$("#tempMin").change( GetGradeTemp );
$("#tempMax").change( GetGradeTemp );
GetGradeTemp();

$('[name="radios"]').on('change',function() {
  // get this value for C or F
  var ThisVal = $(this).val().toLowerCase();
  // input values
  var value1 = $("#tempMin").val();
  var value2 = $("#tempMax").val();
  if(ThisVal == 'c'){
    $("#tempMin").val((value1 - 32) * 5 / 9);
    $("#tempMax").val((value2 - 32) * 5 / 9);
  }else{
    $("#tempMin").val(((value1 * 9) / 5 ) + 32);
    $("#tempMax").val(((value2 * 9) / 5 ) + 32);
  }
  GetGradeTemp();
});
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/css/materialize.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js"></script>

<span class="temp">Min</span><span id="minT" class="min"></span><span class="c_f"></span><span class="max">Max</span><span id="maxT" class="max"></span><span class="c_f"></span>

<div class="row">
  <div class="input-radio col s12 m12 offset-m5 offset-s5" style="margin-bottom: 0;">
          <input type="radio" id="radio1" value="C" name="radios" checked>
          <label classe ="radioLabel" for="radio1">C</label>
          <br>
          <input type="radio" id="radio2" name="radios" value="F">
          <label classe ="radioLabel" for="radio2">F</label>    
  </div>
</div>

<div class="row" id="inline2">
<div class="col s12 m12 l12 offset-m3 offset-l4 temp_uv">
  <div class="quantity">
  <input type="number" id="tempMin" name="temp_min" step="1" min="-60" max="60" placeholder="-5 C" value="-5" />
  </div>
  <div class="line"><strong>—</strong></div>
  <div class="quantity1">
  <input type="number" id="tempMax" name="temp_max" step="1" min="-60" max="60" placeholder="40 C" value="40" />
  </div>
</div>
</div>
<br>
<div class="row">
<div id="temp_Slider_C"></div>

</div>

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

What is the process for converting variables from browser script to Python code?

I ran the script below in my browser webdriver.execute_script("document.getElementsByClassName('bulk_item').length") My goal is to have the number that the script returns stored in a variable called elem for easy access. However, simp ...

Having trouble creating a report with an HTML screenshot using Protractor

Need assistance with generating reports using a html screenshot in Protractor. I have followed all the necessary steps but encountered an error. Any help would be appreciated. Here is my conf.js: // Sample configuration file. var HtmlReporter = require(& ...

The information from AngularJS is not appearing in the table

I am currently developing a web application that utilizes AngularJS for SQL connectivity. While working on my project, I encountered an issue where the data for the "Regional Partner Manager" user is not displaying properly in my table, whereas the data f ...

What is the reasoning behind defaultValue possessing the type of any in TextField Material UI?

According to the Material UI guidelines, the component TextField specifies that its defaultValue property accepts the type any. I decided to experiment with this a bit and found that in practice, defaultValue actually supports multiple types. You can see ...

Removing Embedded Json Element from a Collection in AngularJS HTML

In my JSON Collection, I want to display the Email ID that is marked as IsPreffered = TRUE using AngularJS HTML without using JavaScript. This is my JSON Collection: { "user" : [ { "Name" : "B. Balamanigandan", "Email": [ ...

Showing a hidden div after an unsuccessful AJAX call

Encountering an issue with displaying a div notification using the code snippet below: $.ajax({ url: url, cache: false, async: false, success: function (data) { response = jQuery.parseJSON(data); }, error: functi ...

What could be the reason for the unexpected invisibility of the 4px margin on a static div?

I have a straightforward situation where I am using a fixed positioning <div> that spans the entire width with 100% width. It is designed to have a margin of 4px on all sides. However, for some reason, the right side margin is not visible. Can someon ...

How do you specifically apply an Inset Border Shadow to just two sides using CSS?

Is it possible to apply the inner shadow specifically to the bottom and right sides? I've come across some hacks, but they seem to be more focused on regular border shadows. It's easier to add a drop shadow than to remove an inner shadow. -moz-b ...

Create a concise shorthand representation for margins when styling with jss using Material-UI's theme object

When styling the component, I found a way to apply the margin in a specific manner. style.js const styles = theme => ({ button: { margin: '12px 18px', } }) However, I wanted to utilize material-ui's theme.spacing.unit f ...

Storing an image or video file to the disk of a NodeJS server and creating a reference in MongoDB

Seeking advice on the optimal method to store an image or video file on a NodeJS server and link it to MongoDB. Additionally, I need to enable streaming for the videos. Any recommendations would be greatly appreciated. Thank you! ...

What is the best way to showcase a String variable with spaces in the value field of a form within JSP?

When working with a String variable in JSP and trying to display it in a form field, there might be an issue if the string contains spaces. Only the first word is displayed instead of the entire sentence. Below is the code snippet along with the resulting ...

Leveraging JQuery to extract the numerical value located between the slashes within a hyperlink

I need to extract numeric values from a link like this. For example: /produkt/114664/bergans-of-norway-airojohka-jakke-herre In this case, I want to fetch 114664. To achieve this, I have written the following jQuery code: jQuery(document).ready(functi ...

CSS properties are ineffective in scaling the image strip

I have a large image strip measuring 130560 x 1080. My goal is to load it into an image tag and use the parent div as a viewport, showing only one section of the image at a time. However, I'm encountering a problem when specifying the height and widt ...

Issue with the image posting function in Laravel - why isn't it functioning properly?

Looking for Assistance I've been working on a simple web application using Laravel 6.0 and have encountered an issue with the image post function I developed. Despite not receiving any error messages, the functionality doesn't seem to be work ...

Exploring the best practices for organizing logic within Node.js Express routes

I'm currently utilizing the https://github.com/diegohaz/rest/ boilerplate, but I am unsure about the best practice for implementing logic such as QR code generation and additional validation. My initial thought was to include validation and password ...

Using JavaScript to toggle the display of a label element

Greetings everyone! I recently posted a question on this thread about replacing input with javascript, but ended up abandoning that idea. Instead, I decided to explore a different approach... I made the background of my password field transparent and posi ...

Create a continuous scrolling tool similar to Google Reader for iGoogle

Do you know how to create an infinite scroll widget similar to Google Reader on iGoogle? This widget should be able to dynamically load data as the user scrolls, and replace the traditional scroll bar with a pair of up and down arrows. The HTML structure ...

Guide on sending a JavaScript variable to PHP using AJAX

I am currently utilizing the following JavaScript code to obtain data from a td element when a button is clicked within an onclick event. function rfk(element) { var element = element.parentElement.parentElement; var id = parseInt(element.childre ...

I'm currently facing difficulties trying to implement AJAX with JavaScript and PHP as the desired output is not being

My query is quite straightforward - why isn't the code functioning properly? I am attempting to have the text echoed in PHP displayed inside a div with the ID of "show". Interestingly, this works with a txt file but not with PHP or any other type of f ...

Where can I locate htmlWebpackPlugin.options.title in a Vue CLI 3 project or how can I configure it?

After creating my webpage using vue cli 3, I decided to add a title. Upon examining the public/index.html file, I discovered the code snippet <title><%= htmlWebpackPlugin.options.title %></title>. Can you guide me on how to change and cu ...