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 8114205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:04:52+00:00 2026-06-06T03:04:52+00:00

I am reading contents from JSON file and adding to div with unique IDs.

  • 0

I am reading contents from JSON file and adding to div with unique IDs. I need to call jquery slide down effects to each div. Lets us consider the case, on clicking (div id=A1) it should slide down and show (div id=B1), in that way I have div with IDs A(1..N) and B(1..N).

var i=1;
$.each(items, function (index, item) {

    $(document).ready(function(){
        $("#A"+i).click(function(){
        $("#B"+i).slideToggle();
        });
    });

    $("#allContents").append('<div id="A'+i+'">' + item.Name + '</div>');
    $("#allContents").append('<div id="B'+i+'">' + item.Details + '</div>');
    i++;
});

This is the closest code I could derive to, but it is not working. If anyone could help me fix or suggest a better way to get this thing working it would be great. Thanks a lot!

  • 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-06T03:04:53+00:00Added an answer on June 6, 2026 at 3:04 am
    $('#allContents').on('click', 'div[id^=A]', function() {
        $('div#B' + this.id.replace('A','')).slideToggle();
    });
    

    A little explain

    • div[id^=A] point out those div whose id start with A.
    • this.id retrieve the id of clicked element.
    • this.id.replace('A','') replace A form the id and get the numeric index which equal to index of B.
    • $('div#B' + this.id.replace('A','')) point to element id=B1, id=B2 and so on.

    Full code

    // you can bind event handler outside ot loop
    $('#allContents').on('click', 'div[id^=A]', function() {
        $('div#B' + this.id.replace('A','')).slideToggle();
    });
    
    $.each(items, function(index, item) {
        $("#allContents").append('<div id="A' + i + '">' + item.name + '</div>');
        $("#allContents").append('<div id="B' + i + '">' + item.Details + '</div>');
        i++;
    });
    

    Working Sample


    Note

    As you’re creating div from A(1..N) and B(1..N) dynamically so you need delegate event handler (aka live event).

    Syntax of jQuery .on() for delegate event is like following:

    $(container).on(eventName, target, handlerFunction)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try reading a JSON file from R using rjson but keep getting errors.
I am opening file in binary mode using fopen(abhi.zip,rb); and reading contents from it
I'm reading the contents from an XML file to show the web pages contents,
I have a recursive function reading a table of contents of documents from a
I am reading the contents of the file using fread into an char array.
I need help reading the contents of a webpage. Currently i am using the
I'm reading an xml file using Jaxb, now the contents of the xml file
I'm sorry for complex question. I've got a website that reading contents from MySQL
I've been reading json files from my content folder using the asset manager to
How do I change the contents of a file from this: setenv api1000 Delhi

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.