Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6977715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:39:41+00:00 2026-05-27T17:39:41+00:00

I am a little bit lost. I am getting this JSON: [{ id: 210,

  • 0

I am a little bit lost.
I am getting this JSON:

[{
    "id": "210",
    "name": "Name 1",
    "category": "Category 1"
}, {
    "id": "187",
    "name": "Name 2",
    "category": "Category 1"
}, {
    "id": "186",
    "name": "Name 3",
    "category": "Category 1"
}, {
    "id": "185",
    "name": "Name 4",
    "category": "Category 1"
}, {
    "id": "184",
    "name": "Name 5",
    "category": "Category 1"
}, {
    "id": "183",
    "name": "Name 6",
    "category": "Category 1"
}, {
    "id": "182",
    "name": "Name 7",
    "category": "Category 1"
}, {
    "id": "181",
    "name": "Name 8",
    "category": "Category 2"
}, {
    "id": "180",
    "name": "Name 9",
    "category": "Category 3"
}, {
    "id": "178",
    "name": "Name 10",
    "category": "Category 2"
}]

And I would like to put all of this in a select with options and optgroups. Actually the optgroup should be category

I would like something like this:

<select name="products" class="product" id="product">
<optgroup label="Category 1">
    <option value="210">Name 1</option>
    <option value="187">Name 2</option>
    <option value="186">Name 3</option>
    <option value="185">Name 4</option>
    ...
</optgroup>
<optgroup label="Category 2">
    <option value="181">Name 8</option>
    <option value="178">Name 10</option>
</optgroup>
<optgroup label="Category 3">
    <option value="180">Name 9</option>
</optgroup>

Today I have only made this because I’m struggling too much:

$(document).ready(function () {
    $.getJSON("5.php", {
        val: $(this).val()
    }, function (data) {
        $.each(data, function (i, item) {
            $("<option/>").attr("value", item.id).append(item.name).appendTo("optgroup");
        });
    });
});

As you can see no optgroup 🙂
Is there a way to do this?
I can also modify my JSON if it can make it easier.

Thanks for any help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:39:41+00:00Added an answer on May 27, 2026 at 5:39 pm

    Assuming the optgroups already exist, change this…

    .appendTo("optgroup")
    

    to this…

    .appendTo("optgroup[label='" + item.category + "']");
    

    http://jsfiddle.net/FG9Lg/


    If they don’t exist, you need to create them, though I’d suggest a restructuring of your JSON response to have each item nested under the proper category.

    Like this…

    {
        "Category 1":[
            {"id": "210","name": "Name 1"},
            {"id": "187","name": "Name 2"},
            {"id": "186","name": "Name 3"},
            {"id": "185","name": "Name 4"},
            {"id": "184","name": "Name 5"},
            {"id": "183","name": "Name 6"},
            {"id": "182","name": "Name 7"}
        ],
        "Category 2":[
            {"id": "181","name": "Name 8"},
            {"id": "178","name": "Name 10"}
        ],
        "Category 3": [
            {"id": "180","name": "Name 9"}
        ]
    }
    

    So you could then do this:

    var product = $('#product');
    
    $.each(data, function (key, cat) {
        var group = $('<optgroup>',{label:key});
    
        $.each(cat,function(i,item) {
            $("<option/>",{value:item.id,text:item.name})
                .appendTo(group);
        });
    
        group.appendTo( product );
    });
    

    http://jsfiddle.net/FG9Lg/1/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am getting little bit lost with all the XML handling methods in java,
I lost a little bit of time in this Python for statement: class MyListContainer:
I´m getting a little bit lost in my own (maven enabled) dynamic WEB -
This little bit of syntax has been a bit of a confusion for me
This little bit of code will help me describe my problem: public class Car
Little bit of a 2 parter. First of all im trying to do this
ok let me admit something that I am a little bit lost in here
I am a little bit lost on python iterators. I occasionally use them, but
I'm a little bit lost. I have created a small app that is starting
I'm doing a little bit of work on a horrid piece of software built

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.