Note that there are some explanatory texts on larger screens.

plurals
  1. POJSON object elements listed as undefined (tested both before and after using `jQuery.parseJSON()`)
    text
    copied!<p>I'm trying to populate a dropdown list via iterating through a JSON object and appending the <code>value=</code> of each <code>&lt;option&gt;</code> with <code>row.id</code> and the actual value between the <code>&lt;option&gt;</code> tags with <code>row.name</code>.</p> <p><strong>Edit</strong></p> <p>I forgot to mention that the data being passed has been Ajaxed, and is the result of a PHP call to a database.</p> <p>The problem is that, while the list is populated, each entry is listed as <code>undefined</code>. Ditto for the <code>id</code> as well. I must be doing this wrong, I'm just not sure what. This is the array I have:</p> <pre><code> [{ "id": "1", "0": "1", "parent": "0", "1": "0", "name": "Automotive", "2": "Automotive", "description": null, "3": null, "ordering": "0", "4": "0", "pub lished": "1", "5": "1" }, { "id": "2", "0": "2", "parent": "0", "1": "0", "name": "Aerospace", "2": "Aerospace", "description": null, "3": null, "ordering ": "1", "4": "1", "published": "1", "5": "1" }, { "id": "3", "0": "3", "parent": "0", "1": "0", "name": "Agricultural", "2": "Agricultural", "description": null, "3": null, "ordering": "2", "4": "2", "published": "1", "5": "1" }, { "id": "4", "0": "4", "parent": "0", "1": "0", "name": "Audiovisual", "2": "Audiovisual", "description": null, "3": null, "ordering": "3", "4": "3", "published": "1", "5": "1" }, { "id": "5", "0": "5", "parent": "0", "1": "0", "name": " Construction", "2": "Construction", "description": null, "3": null, "ordering": "4", "4": "4", "published": "1", "5": "1" }, { "id": "6", "0": "6", "parent": "0", "1": "0", "name": "Energy", "2": "Energy", "description": null, "3": null, "ordering": "6", "4": "6", "published": "1", "5": "1" }, { "id": "7", " 0": "7", "parent": "0", "1": "0", "name": "Electronics", "2": "Electronics", "description": null, "3": null, "ordering": "7", "4": "7", "published": "1 ", "5": "1" }, { "id": "8", "0": "8", "parent": "0", "1": "0", "name": "Electrical", "2": "Electrical", "description": null, "3": null, "ordering": "8", "4 ": "8", "published": "1", "5": "1" }, { "id": "9", "0": "9", "parent": "0", "1": "0", "name": "Entertainment", "2": "Entertainment", "description": null, "3": null, "ordering": "9", "4": "9", "published": "1", "5": "1" }, { "id": "10", "0": "10", "parent": "0", "1": "0", "name": "Manufacturing", "2": "Manufa cturing", "description": null, "3": null, "ordering": "9", "4": "9", "published": "1", "5": "1" }, { "id": "11", "0": "11", "parent": "0", "1": "0", "name": "Medical", "2": "Medical", "description": null, "3": null, "ordering": "11", "4": "11", "published": "1", "5": "1" }, { "id": "12", "0": "12", "parent": "0", "1": "0", "name": "Marine", "2": "Marine", "description": null, "3": null, "ordering": "12", "4": "12", "published": "1", "5": "1" }, { "id": "13", "0": "13", "parent": "0", "1": "0", "name": "Home Applicances", "2": "Home Applicances", "description": null, "3": null, "ordering": "13", "4": "13", "published": "1", "5": "1" }, { "id": "14", "0": "14", "parent": "0", "1": "0", "name": "Software", "2": "Software", "description": null, "3": null, "ordering": "14", "4": "14", "published": "1", "5": "1" }, { "id": "15", "0": "15", "parent": "0", "1": "0", "name": "Theoretical work", "2": "Theoretical work", "description": null, "3": null, "ordering": "15", "4": "15", "published": "1", "5": "1" }, { "id": "16", "0": "16", "parent": "0", "1": "0", "name": "Railroad", "2": "Railroad", "description": null, "3": null, "ordering": "16", "4": "16", "published": "1", "5": "1" }, { "id": "77", "0": "77", "parent": "0", "1": "0", "name": "Chemistry", "2": "Chemistry", "description": null, "3": null, "ordering": "5", "4": "5", "published": "1", "5": "1" }, { "id": "158", "0": "158", "parent": "0", "1": "0", "name": "Empty Category", "2": "Empty Category", "description": null, "3": null, "ordering": "17", "4": "17", "published": "1", "5": "1" }] </code></pre> <p>and here's my code:</p> <pre><code>function printSubCategories(categories) { for (var row in categories) { rowParsed = jQuery.parseJSON(row); var opt = document.createElement('option'); if (categories.hasOwnProperty(row)) { opt.text = rowParsed.name; opt.value = rowParsed.id; } jQuery('#subcategories').append(opt); } } </code></pre> <p>What exactly am I doing wrong? </p> <p><strong>Update</strong></p> <p>Tried the following implementation:</p> <pre><code>if (categories.hasOwnProperty(row)) { idParsed = jQuery.parseJSON(row.id); nameParsed = jQuery.parseJSON(row.name) opt.text = idParsed; opt.value = nameParsed; } </code></pre> <p>still not working...</p>
 

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