4

I am trying to create an input with the id and value attributes set from a JSON. I have an ajax call which gets the JSON and the data returned is just fine, and for each object from the JSON I want to create a button with an ID and a value from the JSON.

The Ajax call:

$.ajax({
            type: "GET",
            url: '@Url.Action("GetSubjects", "CommAPI")',
            dataType: "json",
            success: function (data) {
                $.each(data, function (index, element) {
                    $('#subjects').append('<input type ="button" id=" ' + element.Id + ' " value=' + element.Title + '  class=k-button  />');
                });
            },

        });

The objects from the JSON however have 5 additional properties in them, not just Id and Title. When placing the debugger at the line with the input creation, the Id and Title are undefined.

How may I create these inputs from this JSON?

Returned JSON copied from the Console:

14:41:57.928 {"Data":[{"Id":1,"IdCSite":1,"IdDEvent":1,"Title":"Test","CaseName":null,"Description":"dsadasdasda","InsertedDate":"/Date(-62135596800000)/","SelectedUsers":null,"ViewSelectedUsers":null,"IsCurrentUserIncluded":false},{"Id":2,"IdCSite":1,"IdDEvent":1,"Title":"Test2","CaseName":null,"Description":"sdadasdas","InsertedDate":"/Date(-62135596800000)/","SelectedUsers":null,"ViewSelectedUsers":null,"IsCurrentUserIncluded":false},{"Id":3,"IdCSite":1,"IdDEvent":1,"Title":"test 3","CaseName":null,"Description":"sdadasdasda","InsertedDate":"/Date(-62135596800000)/","SelectedUsers":null,"ViewSelectedUsers":null,"IsCurrentUserIncluded":false}],"Total":3,"AggregateResults":null,"Errors":null}1 messageboard:128:25
4
  • Can you post your JSON? Commented Nov 27, 2015 at 12:51
  • try console.log(JSON.stringify(data)) after success. and give data what are you getting Commented Nov 27, 2015 at 12:51
  • JSON posted and copied from the Console guys Commented Nov 27, 2015 at 12:57
  • $.each(data.Data, update to this and it does not have any key named Id Commented Nov 27, 2015 at 12:58

2 Answers 2

5

As per your JSON, you need to use Data property of data object. thus change $.each(data, to $.each(data.Data,

 $.each(data.Data, function (index, element) {
     $('#subjects').append('<input type ="button" id="' + element.Id + '"  value="' + element.Title + '"  class=k-button  />');
 });
Sign up to request clarification or add additional context in comments.

1 Comment

Worked like a charm! Thanks Satpal!
0

You can use directly jQuery properties to make that :

$.ajax({
  type: "GET",
  url: '@Url.Action("GetSubjects", "CommAPI")',
  dataType: "json",
  success: function (data) {
    $.each(data, function (index, element) {
      $input = $('<input'>).prop('id', element.Id).prop('title', element.title).addClass('k'); 
      $('#subjects').append($input);
    });
});

Becareful how do you name your variable, for example 'Id' is a key word of Javascript. You should name like idu if it's a user for example.

Enjoy :)

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.