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

Generating a JSON download link using AngularJS

I'm attempting to generate a link that will enable the download of a JSON file in this way Controller $scope.url = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(obj)); View <a href="url" download="download.json">downlo ...

Determining the element type that was clicked using Angular

Is there a way in Angular to determine the type of element that was clicked? I have successfully implemented this functionality using jQuery, but I'm unsure how to achieve the same outcome in an Angular project. // Click action $(".contactlist").on(" ...

The form post request cannot recognize the undefined variable

When attempting to send values through an HTML form and retrieve it in console.log, I encounter an error that says "TypeError: Cannot read property 'fName' of undefined," despite declaring it in the form name. signin.html : <!doctype html> ...

What is the best way to align all of my content to the center in HTML?

In my web design class, I encountered an issue while creating a page. Currently, my page consists of a navigation bar and an image. I wanted to center both elements using CSS. display: block; margin-left: auto; margin-right: auto I applied this CSS to th ...

If the duration is 24 hours, Moment.js will display 2 days

Looking for a way to allow users to input specific timeframes? For example, 1 week or 5 days and 12 hours. I found that using Duration from Moment.js seemed like the best solution. The snippet of code below is currently giving me 2 00:00, indicating 2 day ...

Modify the c3 axis labels using CSS

I've been using the c3 package in R, which serves as a wrapper for the C3 javascript charting library created by Masayuki Tanaka. One issue I encountered is that my c3 chart was cutting off the last date on the x-axis. After inspecting it in Chrome I ...

Is it advisable to perform several Firestore queries within a single cloud function to minimize round-trip times?

Exploration Within my application scenario, I have a specific screen that displays 8 different lists of items. Each list requires a separate query to Firestore, running asynchronously to retrieve documents from various collections. Through profiling the e ...

Arranging elements within an outer array by the contents of their inner arrays

I need help organizing an array based on the alphabetical order of a specific value within the inner arrays. For example: I want to sort this array by the prefix "old," so old A, old B, etc. const array = [ { personName: "Vans", personTags: ["young", " ...

Customizing the CSS for individual posts in WordPress based on their post

I am interested in establishing a unique look for each individual post on my wordpress.org blog. Is there a way to customize the CSS code of individual posts without changing the design of other posts? I have tried using categories to create a new PHP fi ...

Erase every picture contained within the element

<div id="uniqueidhere"> <span> <span><img src="image1link"></img></span> <span><img src="image2link"></img></span> <span><img src="image3link"></img></span> <span>&l ...

Is it possible for me to repurpose my current Vue component as a single file component?

After working on a Vue app that was directly written into an HTML file served by Django, I am now transitioning to a dedicated Vue.js project using the CLI. As part of this transition, I want to break apart all the components from my single file and move t ...

Change the order of the table data elements using the nth-child CSS selector

I need the second td in my table to appear on top of the first one when I resize the screen. @media screen and (min-width: 200px) and (max-width: 872px) { td :nth-child(1) {display:bottom} td :nth-child(2) {display:top} } <table border="0" cell ...

Material UI TreeView: Organize and present node data with multiple columns in a tree structure

const treeItems = [ { id: 1, name: 'English', country: 'US', children: [ { id: 4, name: 'Spring', country: 'Uk', ...

The CSS scale property is not working as expected when used in a React.js application, specifically

working environment ・next.js ・react ・typescript https://www.youtube.com/watch?v=ujlpzTyJp-M A Toolchip was developed based on the referenced video. However, the --scale: 1; property is not being applied. import React, { FunctionComponent ...

The utilization of useState can potentially trigger an endless loop

Currently, I am in the process of developing a web application using Next.js and Tailwind CSS. My goal is to pass a set of data between methods by utilizing useState. However, I have encountered an issue where the application loads indefinitely with excess ...

Fetching data in React using AJAX

I am in the process of developing a React Component that will display data retrieved from an AJAX call. Here's my scenario - I have a Jinja Flask back end hosted on AWS API Gateway, which requires custom headers and the Authorization header to serve H ...

Arranging xCharts based on the weekday order

Struggling with xCharts, specifically trying to display a bar chart showing numbers with corresponding days of the week in order. Despite my efforts, I can't seem to get them to appear in the correct sequence. Refer to the image below for reference: ...

Issue resolved: Mysterious fix found for background images not displaying in NextJS React components

I am having trouble displaying a background image on an element in NextJs using Typescript and Tailwind. I do not believe it is a TypeScript issue since I am not receiving any errors or IntelliSense warnings. Below is the code I am working with: var classn ...

Initial argument for the event listener

If I have event handlers registered inline in my markup (even though it's deprecated) like span id="..." onclick="foo(p1,p2,p3)" how do I access the "event" object in the event handler function foo? Is changing the above to span ...

I'm encountering difficulties utilizing ternary operators in TypeScript

I am struggling with ternary operators in TypeScript and need help understanding the issue. Please review the code below: const QuizQuestionContainer = ({ qa }: QuizQuestionContainerPropsType) => { const { question, option1, option2, option ...