0

I am generating radio button depends on jsondata id and name. but below code is not working.how to resove this issue.

Json:

{
    "a": [
            { "id" : "a1", "family" : "Family" },
            { "id" : "a2", "family" : "Family" },
            { "id" : "a3", "family" : "Family" }
        ],
    "b": [
            { "id" : "b1", "family" : "Family" },
            { "id" : "b2", "family" : "Family" },
            { "id" : "b3", "family" : "Family" }
        ],
    "c": [
            { "id" : "c1", "family" : "Family" },
            { "id" : "c2", "family" : "Family" },
            { "id" : "c3", "family" : "Family" }
        ]
}

Javascript:

generateFamily("a");

function generateFamily(objt) {
    var Objct = objt;
    $.getJSON("./js/result.json", function(result) {
        var testobj = result + "." + Objct;
        $.each(testobj, function(i, field) {
            var char = "a";
            char = "ln-" + char;
            $("#dFour").append("<li class=" + char + " style='display: list-item;'><a href='#'><span class='m'><input type='radio' class='fbol_ff' value=" + field.id + " family=" + field.family + "/></span>" + field.family + "</a></li>");
        });
    });
}
1
  • what is the error showing in the console? Commented Jan 28, 2016 at 11:58

2 Answers 2

2

You nee to read the a field from JSON data. Since you are passing a in a variable i.e. objt. You can fetch that using Bracket notation

var testobj=result[objt];

instead of

var testobj = result + "." + Objct;

var result = {
  "a": [{
    "id": "a1",
    "family": "Family"
  }, {
    "id": "a2",
    "family": "Family"
  }, {
    "id": "a3",
    "family": "Family"
  }],
  "b": [{
    "id": "b1",
    "family": "Family"
  }, {
    "id": "b2",
    "family": "Family"
  }, {
    "id": "b3",
    "family": "Family"
  }],
  "c": [{
    "id": "c1",
    "family": "Family"
  }, {
    "id": "c2",
    "family": "Family"
  }, {
    "id": "c3",
    "family": "Family"
  }]
};


generateFamily("a");

function generateFamily(objt) {
  var testobj = result[objt];
  $.each(testobj, function(i, field) {
    var char = "a";
    char = "ln-" + char;
    $("#dFour").append("<li class=" + char + " style='display: list-item;'><a href='#'><span class='m'><input type='radio' class='fbol_ff' value=" + field.id + " family=" + field.family + "/></span>" + field.family + "</a></li>");

  });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='dFour'></div>

Sign up to request clarification or add additional context in comments.

1 Comment

@supermans, See code snippet, I have used JSON object in example
0

Use the length of json in the loop.

generateFamily("a");

function generateFamily(objt) {
    result = $.getJSON("dta_text.json", function(result) {
        $.each(result, function(i, field) {
            var char = "a";
            char = "ln-" + char;
            for (i = 0; i < Object.keys(result).length; i++) {
                 $("#dFour").append("<li class=" + char + " style='display: list-item;'><a href='#'><span class='m'><input type='radio' class='fbol_ff' value=" + field[i].id + " family=" + field[i].family + "/></span>" + field[i].family + "</a></li>");
            }
        });
    });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.