facebook

How to get a radio Button to determine a specific function

  1. MobiOne Archive
  2.  > 
  3. Getting Help – General
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #344491 Reply

    Hi,
    I am including a calculator in my app, and I want to be able to provide the results in either lb Pounds, or kg Kilograms.
    I have the java script working for both, but I would like just one set of input fields, with the results dependent on which radio button the user selects.

    ie if user selcets radio button for inches & pounds, they can enter measurements in inches, press ‘calculate’ and have the results show up in Pounds. The same for if the user selects the centimetres & kilograms radio button.

    My javascript thus far works for the individual results kg or lb and is as follows;

    function calculatekg() {
    var girth1_value = document.getElementById(‘m1-weightcalc-girth1’).value;
    var length1_value = document.getElementById(‘m1-weightcalc-length1’).value;
    var unit = ” kg”;
    document.getElementById(‘m1-weightcalc-resultkg’).value = (girth1_value*length1_value*girth1_value*0.000254).toFixed(2) + unit;
    }

    /**
    * Notification that the page’s HTML/CSS/JS is about to be loaded.
    * Perform custom logic here, f.e. you can cancel request to the server.
    * @param {String} targetScreenId
    * @returns {boolean} true to continue loading; false to halt loading
    */
    phoneui.prePageLoad = function(targetScreenId) {
    // add custom pre-load code here
    // return false to terminate page loading, this cancels transition to page as well
    return true;
    }/**
    * Called when document is loaded.
    */
    phoneui.documentReadyHandler = function() {
    }

    function calculatelb() {
    var girth1_value = document.getElementById(‘m1-weightcalc-girth1’).value;
    var length1_value = document.getElementById(‘m1-weightcalc-length1’).value;
    var unit = ” lb”;
    document.getElementById(‘m1-weightcalc-resultlb’).value = (girth1_value*length1_value*girth1_value/300).toFixed(2) + unit;
    }

    /**
    * Notification that the page’s HTML/CSS/JS is about to be loaded.
    * Perform custom logic here, f.e. you can cancel request to the server.
    * @param {String} targetScreenId
    * @returns {boolean} true to continue loading; false to halt loading
    */
    phoneui.prePageLoad = function(targetScreenId) {
    // add custom pre-load code here
    // return false to terminate page loading, this cancels transition to page as well
    return true;
    }/**
    * Called when document is loaded.
    */
    phoneui.documentReadyHandler = function() {
    }

    I am VERY new to java script, and i have put the above together with trial and error, adding rounding & units to it, until it worked. I am happy to learn, and willing to play around with it.

    I am still searching through the web for examples, where I can try components to make this work, but I think it will be a little more complicated than the simple functions I am using thus far.

    Thank you in advance for any input

    🙂

    Attachments:
    You must be logged in to view attached files.
    #344970 Reply

    Hi living.horses,

    You need a function that check the radiogroup’s value, then use this value to know what function needs to be executed, f.e.

    function checkRadioValues(){
    
        var radioSelected = $('input[name="radio-group1"]:checked').val(); 
        
        if(radioSelected=="radio1"){
            
            function1();
        }
        else{
            function2();
        }
        
    }

    And your functions would be the ones that you already have. Since you are not using a button you can call this function when user clicks on radiobuttons setting the On click function as Run Javascript code. See screenshot:
    See attachment radioConfig.png

    Attachments:
    You must be logged in to view attached files.
    #346400 Reply

    thank you, i will give it a go 🙂

Viewing 3 posts - 1 through 3 (of 3 total)
Reply To: How to get a radio Button to determine a specific function

You must be logged in to post in the forum log in