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

  • Home
  • SEARCH
  • 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 7875699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:02:04+00:00 2026-06-03T03:02:04+00:00

I am a new to JSON, I am trying to make the menu that

  • 0

I am a new to JSON, I am trying to make the menu that can click and display the data.
All the objects are return from the Controller of my asp.net mvc project.

This is my code :

<script language="javascript" type="text/javascript">
  function ViewProduct() {
    var searchVal = $("#txtsearch").val();
    $.getJSON(url, function (data) {
      var mainMenu = $("#content ul#navmenu-v");
      $.each(data, function (index, dataOption) {
        var new_li = $("<li class='level1' id='select_list'><a href='javascript:void(0);' id='" + dataOption.ID + "' class ='selectedcategory'>" + dataOption.Name + "</a>");
        mainMenu.append(new_li);
        $('a#' + dataOption.ID).click(function () {
        var urlGetProByDep = '<%: Url.Content("~/") %>' + "Products/GetProductsByDepList";
        t = dataOption.ID;
        var data = {};
        if (searchVal != "") {
          data.depID = t;
          data.Q = searchVal;
        } else {
          data.depID = t;
        }
      $(".brand_id").empty();
      $.getJSON("ProductListing/Index", data, function (product) {
         $(".brand_id").empty();
         $.getJSON("ProductListing/Index", data, function (product) {
            $.each(product.ja, function (index, value) {
               $(".brand_id").html( value.Name + "</br>" + value.PictureName + "</br>" + value.ID);
            });
         });
      });
   });
}

$(document).ready(function () {
   ViewProduct(); 
});

The other block of menu code works very well , just have a problem with this block of my code above :

      $.getJSON("ProductListing/Index", data, function (product) {
         $(".brand_id").empty();
         $.getJSON("ProductListing/Index", data, function (product) {
            $.each(product.ja, function (index, value) {
               $(".brand_id").html( value.Name + "</br>" + value.PictureName + "</br>" + value.ID);
            });
         });
      });

It is the block that I take the the object of JSON to display the in my view, but when I click on the menu , It shows only the element at the last of JSON object.

This is my JSON :

{"ja":
   [
       {"Name":"ABC1","PictureName1":"my image name1","ID":1},   
       {"Name":"ABC2","PictureName2":"my image name2","ID":2}
]}

Each menu is the department of each product. And if I do not loop the product data in the menu block, I cannot take the ID of the department to query to get the product in each department.

Any idea or question are welcome, Thanks you for taking your time.

  • 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-06-03T03:02:06+00:00Added an answer on June 3, 2026 at 3:02 am

    You seem to have a copy&paste-error in your code, you will do 2 request to exactly the same url after each other – not even using the results of the first request. I guess you only wanted something like

    $(".brand_id").empty();
    $.getJSON("ProductListing/Index", data, function (product) {
        $.each(product.ja, function (index, value) {
             $(".brand_id").html( value.Name + "</br>" + value.PictureName + "</br>" + value.ID);
        });
    });
    

    But your problem is the .html() method. You are looping over your products, and in each turn you set the html of each .brand_id-element to a new value. So the result you see is what you have set to all elements in the last iteration. What you would need to do is set the HTML of each element on its own, each with a different content.

    Yet, instead of emptying the list items and readding new contents to them (maybe the number of your products changes each query request), I’d rather remove all items and append new ones:

    $(".brand_id").remove();
    $.getJSON("ProductListing/Index", data, function (product) {
        var $parent = $("#parent"); // the parent node of the product list
        $.each(product.ja, function (index, value) {
             $parent.append('<li class="brand_id">' + value.Name + '<br />' + value.PictureName + '<br />' + value.ID + '</li>');
        });
    });
    

    (edit) …or, if brand_id really is an id:

    var $parent = $("#brand_id").empty();
    $.getJSON("ProductListing/Index", data, function (product) {
        $.each(product.ja, function (index, value) {
             $parent.append('<div>' + value.Name + '<br />' + value.PictureName + '<br />' + value.ID + '</div>');
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make an app that parses data from Twitter using JSON
I am trying to make an ajax sending data in JSON from a partial
I'm new to Ruby. I'm trying to make an app that reads from a
I'm trying to make a PhoneGap application that gets data from MYSQL with PHP
I'm trying to send JSON data (demographics) to a new page (within the same
I'm trying to make a datatable using YUI with JSON returned data. Included is
I'm trying to make a page that my mobile phone app can hit to
I am trying to make my WCF service method to return JSON-object, but it
I am trying to build a webapp that gets data from server and shows
I'm new with JSON.NET and I'm trying to deserialize a JSON string to a

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.