Home > Blockchain >  Convert mm to cm with jQuery | Gravity Form
Convert mm to cm with jQuery | Gravity Form

Time:09-27

Why doesn't the below work, and how should I do this?
Do you have a better and shorter way to write this code?
".Select_unit" is the class name of Radiobutton (I learned from User Rochelle :) Thank You!)


My Gravity Form: enter image description here


$(function() {

var W_val;
var H_val;

      //set width textbox Value to W_val
    $('#input_2_1').keyup(function() {
      W_val = $("#input_2_1").val();
    });

      //set Height textbox Value to H_val
    $('#input_2_2').keyup(function() {
      H_val = $("#input_2_2").val();
    });


     //Change Unit mm to cm
$('.Select_Unit input[type="radio"]').on('change',function(){
     
    var radioValue = $('input[name="#input_2_3"]:checked').val();

    if (radioValue=="cm")
        {
            $("#input_2_1").val(W_val);
            $("#input_2_2").val(H_val);
        }
    else
        {
            $("#input_2_1").val((W_val*10));
            $("#input_2_2").val((H_val*10));
        }

    });

});

CodePudding user response:

In your html, there is a div

<div class='gfield_radio' id='input_2_3'>

as this is a div, it's not an input so the selector $('input[name="input_2_3"]') returns no inputs, so .val() is always undefined.

var radioValue = $('input[name="input_2_3"]:checked').val();

the radioValue selector needs to be:

var radioValue = $('#input_2_3 input:checked').val();

Then it works: https://jsfiddle.net/57rx2guo/

CodePudding user response:

Thank You!

var radioValue = $('input[name="input_3"]:checked').val();
  • Related