0

I have gone mad trying to bind the json string to the dropdown. Please can somebody help??

My dropdownlist id is 'ddlteams'

My json string is in this form :

"{"TeamList" : [{"teamid" : "1","teamname" : "Barcelona"}]}"

so first i convert into an object.

var objdata = $.parseJSON(jsonObj.d);

Then i add my code given below.

$('#ddlteams').children().remove();

I am having problem with the below code. Please can somebody help me with this

$.each(objdata, function(i, val) {
    $('#ddlteams').append('<option value="' + objdata.TeamList[0].teamid + '">' + TeamList[0].teamname + '</option>');
});
4
  • Assuming objdata is the parsed JSON, i will be "TeamList" and val will be the array. Have a look at the documentation of each. Just iterate normally over objdata.TeamList. Commented Feb 6, 2012 at 19:20
  • Hi Felix, sorry but i did not get you. do i need to replace i with "TeamList"?? is val a keyword in there?? Commented Feb 6, 2012 at 19:24
  • You are using $.each but it does not seem you know how it works. It will iterate over each property of the object or element of the arrays passed. The property name and its value are passed to the callback function as arguments, in your case i and val. Commented Feb 6, 2012 at 19:29
  • Hi Felix. agree with you. started learning jquery very recently. i ll try that! Commented Feb 6, 2012 at 19:34

1 Answer 1

4

try

$.each(objdata["TeamList"], function(i, val) {
    $('#ddlteams').append('<option value="' + val.teamid + '">' + val.teamname + '</option>');
});
Sign up to request clarification or add additional context in comments.

4 Comments

Perhaps $('#ddlteams').append('<option value="' + val['teamid'] + '">' + val['teamname'] + '</option>');? I'll try to reproduce what your working with and help you get it running.
val['teamid'] is the same as val.teamid.
Hi.. We are very near. I have some 5 items to be displayed. I did this -> $.each(objdata, function(i, val) { $('#ddlteams').append('<option value="' + val[0].teamid + '">' + val[0].teamname + '</option>'); }); and i got the first result. Now to get the other items i tried to declare a variable and assign it to 0 and then incremented it by 1. but its not working
You don't mean to iterate through each of objectdata you mean to iterate through ojectdata.TeamList. Look again at the answer I gave.

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.