1

I previously posted a version of this question (JS - Constructor referencing) and found out the answer within the duplicates, especially Access / process (nested) objects, arrays or JSON. From here, I'm still having a ridiculous amount of time getting this to work as intended.

Given the following example code:

    function f1(args) {
        this.defaults = {
            param1 : 100,
            param2 : 900;
        }
        this.ranges = {
            param1 : { min : 0, max : 500 },
            param2 : { min : 0, max : 1000 };
        }
        this.steps = {
            param1 : 50,
            param2 : 100;
        }
    var myF1 = new f1();

I've read up on the previous duplicates and learning accessing the unknown property names by:

    for (var propertyName in myF) {
        newName = propertyName;
    }

What I need to do is cycle through the function (or any function) and build HTML5 range inputs for the function (keep in mind that the parameter names will be greatly different or random, so I assume I need to loop for the key itself. So, in the case of f1/myF1, the generated HTML5 would be:

    param1: <input type="range" name="param1" min="0" max="500" steps="50" />
    param2: <input type="range" name="param2" min="0" max="1000" steps="100" />

Note: The values for range=, name=, min=, max= and step= are all dynamically generated from the function's properties.

I've tried looping for keys, calling for values and pretty much any other research and attempt I can, but I always end up with variables overwriting themselves and I'm guessing this is just more in the deep end than this amateur swimmer can figure out.

Also, realize that each parameter will have a defaults, ranges and steps, so it is not like we have to worry about THAT level of randomization or variation. Thank you SO-JS gurus!

Community
  • 1
  • 1
user3223880
  • 65
  • 1
  • 10
  • If I am not wrong, basically you want generated html(input) by calling `f1()` inside loop. Of course the different values on input would be dynamic? – Suman Bogati Jan 26 '14 at 05:35
  • I can generate the HTML, etc. I just cannot seem to properly get the parameter names from `defaultValues` and the subsequent for `ranges` and `steps`. If I can GET the data, I can do with it as needed for the HTML5 and other use. So, to answer your question more or less: yes. – user3223880 Jan 26 '14 at 06:59

2 Answers2

1

First of all, there are two errors in your example.

  1. the opening bracket of function f1(args) { is not closed.
  2. the semicolons of the last entry in this.defaults and this.steps should not be there.

Following code should (relying on jQuery $) create two correctly working input elements of type range though the code strongly relies on the given data format. One input element is getting the value by looping myF1.steps properties and each name is tried on defaults and ranges. The helper function applyAttr safely accesses the values by another propertyName or direct value.

Working plunker

$(document).ready(function() {

  function f1(args) {
    this.defaults = {
        param1 : 100,
        param2 : 900
    }
    this.ranges = {
        param1 : { min : 0, max : 500 },
        param2 : { min : 0, max : 1000 }
    }
    this.steps = {
        param1 : 50,
        param2 : 100
    }
  }
  var myF1 = new f1();

  function applyAttr(inElem, data, propName) {
    if(data.hasOwnProperty(propName))
      inElem.prop(propName, data[propName]);
    else {
      inElem.prop(propName, data);
    }
  }

  for(var stepsProp in myF1.steps) {
    var inputElem = $('<input type="range"/>');
    inputElem.prop('step', myF1.steps[stepsProp]);
    if(myF1.defaults.hasOwnProperty(stepsProp)) {
      inputElem.prop('value', myF1.defaults[stepsProp]);
    }
    if(myF1.ranges.hasOwnProperty(stepsProp)) {
      applyAttr(inputElem, myF1.ranges[stepsProp], 'min');
      applyAttr(inputElem, myF1.ranges[stepsProp], 'max');
    }
    $('body').append(inputElem);
  }
});
angabriel
  • 4,979
  • 2
  • 35
  • 37
  • +1 for the completeness and conciseness, but the question remains in how to do so in a native way without framework (jQuery or otherwise)? – user3223880 Jan 26 '14 at 06:54
1

It looks like you would rather iterate over the properties of myF1.defaults, myF1.ranges or myF1.steps, not over the properties of myF1 itself (because they are fixed).

I don't know what you have tried, but this is how you could create the DOM elements you want:

var myF1 = new f1();
var elements = [];

for (var param in myF1.ranges) {
    var input = document.createElement('input');
    input.type = 'range';
    input.name = param;
    input.min = myF1.ranges[param].min;
    input.max = myF1.ranges[param].max;
    input.step = myF1.steps[param];
    input.value = myF1.defaults[param];

    var span = document.createElement('span');
    span.appendChild(document.createTextNode(param + ': '));
    span.appendChild(input);
    elements.push(span);
}

Simply iterate over the properties of ranges and access the same properties of steps and defaults. Then you can do whatever you want with elements. DEMO

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • +1 for the swiftness and my building mancrush on Felix's knowledge. Doing it this way with `var param` will properly get the name of the defaultValue, rather it be `param1`, `param2`, or `ksid83isozn`, correct? – user3223880 Jan 26 '14 at 06:56
  • I'm not sure what you mean by "get the name of the defaultValue". `param` will have the values `param1` and `param2` because those are the properties in `myF1.ranges`. – Felix Kling Jan 26 '14 at 07:02
  • Meaning whatever the name tied to the value is within the property, that is what will pulled? In this case the name was `param1`, so if this name was something different (or this executed on a similar function/object with totally different names) it would work the same? – user3223880 Jan 26 '14 at 07:04
  • Sure. If you you have `this.ranges = {a: ..., b: ..., c: ...}`, then `param`'s values will be `'a'`, `'b'`, `'c'` :) – Felix Kling Jan 26 '14 at 07:06
  • I missed the fiddle which answers my own comment. Thank you for your time! – user3223880 Jan 26 '14 at 07:06
  • @Felix: You fixed two errors which are still present in the OP's question. Will it be ok to leave them there? – angabriel Jan 26 '14 at 11:47