0

This is json data is comming from php

[{"employee_id":"1","first_name":"Ganesh","last_name":"Mulay"},
 {"employee_id":"2","first_name":"khalid","last_name":"shaikh"},
 {"employee_id":"3","first_name":"Navnath","last_name":"Bangar"}
]

And i want output like this

<option value="1"> Ganesh Mulay </option>
<option value="2"> khalid shaikh </option>
<option value="3"> Navnath Bangar</option>
1

3 Answers 3

0

It is as same as using javascript

for (var i = 0; i < arr.length; i++) // Do something like arr[i].employee_id;

Kindly check this example: https://jsfiddle.net/14ra68hs/

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

2 Comments

I believe you may measured the length of JSON string
@GaneshMulay You are welcome :) Would you mind to accept mine as your answer if it solved your problem
0
var jData = [{
  "employee_id": "1",
  "first_name": "Ganesh",
  "last_name": "Mulay"
}, {
  "employee_id": "2",
  "first_name": "Rahul",
  "last_name": "kumar"
}, {
  "employee_id": "3",
  "first_name": "Kiran",
  "last_name": "Raj"
}];

$('<select id="test">').appendTo('body');
$.each(jData, function(index, value) {
  $('<option>').val(value.employee_id).text(value.first_name + ' ' + value.last_name).appendTo('select#test');
});

var jData = [{
  "employee_id": "1",
  "first_name": "Ganesh",
  "last_name": "Mulay"
}, {
  "employee_id": "2",
  "first_name": "Rahul",
  "last_name": "kumar"
}, {
  "employee_id": "3",
  "first_name": "Kiran",
  "last_name": "Raj"
}];

$('<select id="test">').appendTo('body');
$.each(jData, function(index, value) {
  $('<option>').val(value.employee_id).text(value.first_name + ' ' + value.last_name).appendTo('select#test');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

JS Fiddle

Comments

0

You can do something like this

$(document).ready(function(){

var json =[{"employee_id":"1","first_name":"Ganesh","last_name":"Mulay"},{"employee_id":"2","first_name":"Rahul","last_name":"kumar"},{"employee_id":"3","first_name":"Kiran","last_name":"Raj"}
];


//convert json to object


var html = "";

for(var key in json)
{
  
  html += '<option value="'+json[key].employee_id+'">'+
json[key].first_name+ ' '+json[key].last_name+'</option>';

}
  
 $("select").html(html)

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

<select>

</select>

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.