Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get the value of n-th item in a JSON list (using JQuery)?
    primarykey
    data
    text
    <p>How do I retrieve the <em>n</em>-th value in a JSON file (and maybe it's children, too)?</p> <p>For example, I have a collection of courses and associated events formatted in JSON, which I'm using to populate a couple of select lists, e.g:</p> <pre><code>// extract from courselist.php [{ "optionValue": "Getting Research into Practice", "events": [ {"date": "29 October"} ] }, { "optionValue": "Human Resources for Managers", "events": [ {"date": "September 1"}, {"date": "November 2"} ] }] </code></pre> <p>I'm doing this to create the form from that JSON:</p> <pre><code>$(function(){ $("select#coursetype").change(function(){ $.getJSON("courselist.php",{id: $(this).val(), ajax: 'true'}, function(data){ var options = ''; //item in select list options += '&lt;option value=""&gt;&lt;/option&gt;'; for (var i = 0; i &lt; data.length; i++) { options += '&lt;option value="' + [i] + '"&gt;' + data[i].optionValue + '&lt;/option&gt;'; $("#courselist").html(options); $('#courselist option:first').attr('selected', 'selected'); }; }) }) }); </code></pre> <p>..which outputs HTML along the lines of:</p> <pre><code>&lt;select id="coursetype" name="coursetype"&gt; &lt;option value="0"&gt;Getting Research into Practice&lt;/option&gt; &lt;option value="1"&gt;Human Resources for Managers&lt;/option&gt; &lt;/select&gt; </code></pre> <p>Say, the user selects 'Human Resources for Managers' (value="1"), and $_POSTs the form, how can I later retrieve that course name and it's event info from the POST value? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload