0

Below is the code I'm using to retrieve a response from a server page using js:

<body>
    <div id="content">

    </div>
    <script>
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (xhttp.readyState == 4 && xhttp.status == 200) {
            document.getElementById("content").innerHTML = xhttp.responseText;
        }
    };
    xhttp.open("GET", "https://api.certicasolutions.com/items/?x-ic-credential=xyz", true);
    xhttp.send();
    </script>
</body>

and following is the json response

{
    "completed":true,

    "totalItems":98891,

    "items" : [
    {
        "ia_biserial":"",
        "ia_bloomstaxonomy":"Creating",
        "ia_correctanswer":"",
        "ia_difficulty":"High",
        "ia_dok":"IV",
        "ia_extid":"231617",
        "ia_gradelevel":"GradeK",
        "ia_hasimages":"False",
        "ia_itemid":1,
        "ia_lang":"English",
        "ia_pointvalue":"2",
        "ia_pvalue":"",
        "ia_subject":"Math",
        "ia_teitype":"OR",
        "ia_vendorid":"i-321813",
        "passages":[],
        "standards":[]
    },
    {
        "ia_biserial":"",
        "ia_bloomstaxonomy":"Creating",
        "ia_correctanswer":"",
        "ia_difficulty":"High",
        "ia_dok":"IV",
        "ia_extid":"231616",
        "ia_gradelevel":"GradeK",
        "ia_hasimages":"False",
        "ia_itemid":2,
        "ia_lang":"English",
        "ia_pointvalue":"2",
        "ia_pvalue":"",
        "ia_subject":"Math",
        "ia_teitype":"OR",
        "ia_vendorid":"i-321812",
        "passages":[],
        "standards":[]
    },

I want to display only item id's "ia_itemid" in a select drop down list using jQuery.

4
  • Use JSON.parse to turn the response in to an array of objects, then loop through that array and populate the select as needed. Commented Jun 16, 2016 at 11:17
  • i'm new to jquery can you please give me an example thank you Commented Jun 16, 2016 at 11:17
  • 3
    "using jquery" I'll just note that you're not using jQuery in your code. It'd be a fair bit shorter if you did. Commented Jun 16, 2016 at 11:17
  • 1
    This is a duplicate of get json data with jquery (and many, many more of those) and From an array of objects, extract value of a property as array. The answers to those questions answer this question. Commented Jun 16, 2016 at 11:20

2 Answers 2

0

i want to display only item id's "ia_itemid" in a select drop down list using jquery

I cannot see the drop down DOM ,instead only ajax & it's response

If you want to get ia_itemid you can use forEach array method and create an array containing value of ia_itemid

    var a = {
   "completed":true,
   "totalItems":98891,
   "items": [
        {"ia_biserial":"",
    "ia_bloomstaxonomy":"Creating"
    ,"ia_correctanswer":"",
    "ia_difficulty":"High",
    "ia_dok":"IV",
    "ia_extid":"231617",
    "ia_gradelevel":"GradeK",
    "ia_hasimages":"False",
    "ia_itemid":1,
    "ia_lang":"English",
    "ia_pointvalue":"2",
    "ia_pvalue":"",
    "ia_subject":"Math",
    "ia_teitype":"OR","ia_vendorid":"i-321813",
    "passages":[],
    "standards":[]
     },

    {
  "ia_biserial":"",
    "ia_bloomstaxonomy":"Creating",
    "ia_correctanswer":"",
    "ia_difficulty":"High",
    "ia_dok":"IV",
    "ia_extid":"231616",
    "ia_gradelevel":"GradeK",
    "ia_hasimages":"False",
    "ia_itemid":2,
    "ia_lang":"English",
    "ia_pointvalue":"2",
    "ia_pvalue":"",
    "ia_subject":"Math",
    "ia_teitype":"OR",
    "ia_vendorid":"i-321812",
    "passages":[],
    "standards":[]
    },
    ]
}

var _getItem = a.items;
var _filter = []
_getItem.forEach(function(item){
           _filter.push(item.ia_itemid)
})
var _options =""; 
// loop through _filter and create options
_filter.forEach(function(item){
   _options +='<option value="demo_'+item+'">'+item+'</option>'
})
$("#demoApp").append(_options); //Append with select element

JSFIDDLE

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

Comments

0

Usually it's best to break your problem down into parts and then research each part. In your case, you need to:

Basically:

// Retrieve the data as JSON and parse it (automatically)
$.getJSON("https://api.certicasolutions.com/items/?x-ic-credential=xyz", function(data) {
    // Get the options for your select box
    var options = $("selector-for-your-select-box")[0].options;

    // Clear any old ones
    options.length = 0;

    // Add the new ones
    data.items.forEach(function(item) {
        options.add(new Option(item.ia_itemid));
    });
});

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.