• Resolved francinegg

    (@francinegg)


    Hello! I’m trying to make a converter that will update the calculated fields of several weight values at once. It would ideally work onkeyup.

    The first form on this page is the kind of thing I mean: https://www.w3schools.com/howto/howto_js_weight_converter.asp

    But unfortunately W3 only explain how to do the javascript for one at a time!

    This is the function I came up with, but it doesn’t seem to work at all:

    (function(){
    if (fieldname4>0) return fieldname4/0.45359237;
    if (fieldname6>0) return fieldname6/14;
    return fieldname5;
    })()

    I think the main problem is it gets confused trying to do both/all at once.

    Any ideas?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author codepeople

    (@codepeople)

    Hello @francinegg,

    Please, would be much better if you indicate the URL to the webpage where your form is inserted, and describe the situation using the names of fields in the form, because I don’t know who are the fields: fieldname4, fieldname6, or fieldname5 in the equation you sent as reference.

    Best regards.

    Thread Starter francinegg

    (@francinegg)

    Sorry, that was stupid of me.

    https://www.guidesglobal.com/converters-2/

    So as you can see I’ve made the simpler converter (temperature) work quite nicely. It’s when I have more than two fields that I hit the ceiling of my javascript skills!

    Function key:
    fieldname4 = kg
    fieldname5 = lb
    fieldname6 = st

    Faulty function in lb (fieldname5):
    (function(){
    if (fieldname4>0) return fieldname4/0.45359237;
    if (fieldname6>0) return fieldname6/14;
    return fieldname5;
    })()

    • This reply was modified 6 years, 4 months ago by francinegg.
    • This reply was modified 6 years, 4 months ago by francinegg.
    Plugin Author codepeople

    (@codepeople)

    Hello @francinegg,

    The situation here is complex, because all fields are calculated fields and any of them modifies the others, so, you will need some tricks:

    Insert a “HTML Content” field in the form with the piece of code:

    <script>
    jQuery(document).on('keydown', '[id*="fieldname4_"]', function(){ editing='kg';});
    jQuery(document).on('keydown', '[id*="fieldname5_"]', function(){ editing='lb';});
    jQuery(document).on('keydown', '[id*="fieldname6_"]', function(){ editing='st';});
    </script>

    The global variable editing, allows to identify from the equation the field that is being entered.

    And then, edit the equations as follows:

    For the fieldname4:

    IF(OR(!('editing' in window), editing == 'kg'), __ME__, IF(editing == 'lb', fieldname5*0.45359237, fieldname6*6.35029318))

    For the fieldname5:

    IF(OR(!('editing' in window), editing == 'lb'), __ME__, IF(editing == 'kg', fieldname4/0.45359237, fieldname6/14))

    For the fieldname6:

    IF(OR(!('editing' in window), editing == 'st'), __ME__, IF(editing == 'kg', fieldname4/6.35029318, fieldname5/14))

    and that’s all.

    • This reply was modified 6 years, 4 months ago by codepeople.
    Thread Starter francinegg

    (@francinegg)

    Perfect! Thank you so much, that’s very kind of you.

    Plugin Author codepeople

    (@codepeople)

    Hi,

    It has been a pleasure.

    Best regards.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Making a weight converter’ is closed to new replies.