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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:46:59+00:00 2026-06-10T23:46:59+00:00

Probably beginner jquery question… Here is my problem: I have dynamically created list of

  • 0

Probably beginner jquery question… Here is my problem:

I have dynamically created list of div elements. Each has its’ specific content, id and a toggle icon in the corner. When I press toggle icon it causes the following: expands/hides the content of the div and changes the icon.

My initial thinking was to call 2 custom jquery functions something like this (the number is dynamically inserted by PHP):

<div id="div152" >
  <div id="content152">some content</div>
  <a id="toggle-icon152" href="javascript:void(0);" onclick="togglemydiv(content152); changeicon(icon152)">
    <div id="icon152">someicon</div>
  </a>
</div>

Now this works in some browsers but obviously it is not how it should be done as it doesn’t work in FF in windows or in fiddle. Any help would be appreciated.

  • 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-10T23:47:00+00:00Added an answer on June 10, 2026 at 11:47 pm

    I suggest that you don’t hardcode your ids into js.

    Here is a suggestion how to do it simpler:

    html:

    <div id="div152" >
      <div id="content152">some content</div>
      <a id="toggle-icon152" data-bind-action="toggle-content" href="#">
        <div id="icon152">someicon</div>
      </a>
    </div>​
    

    js:

    $("a[data-bind-action='toggle-content']").click(toggleContent);
    
    function toggleContent(){
        var a = $(this);
        var div = a.prev();
        div.toggle();
        e.preventDefault();
        return false;
    }
    

    Se how it works on jsfiddle.

    Here is version which does not assume your content is prev element to a, but you add attribute to your link so it knows what to toggle: http://jsfiddle.net/PrmX5/4/

    I have added html5 data- attribute to identify anchor to which toggle function needs to be attached from js, you can do this different, of course, using class or other (faster?) jQuery selector.

    As @coosal said in its answer, if divs are dynamically generated (ajax or on client), you need to use live instead of binding on document.ready, so here is version which uses live: http://jsfiddle.net/PrmX5/5/

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

Sidebar

Related Questions

Probably a very basic beginner question. Imagine the following situation: I have an ASP.NET
A beginner's question, probably a trivial one. Here's the XUL code snippet: <window xmlns=http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul>
Ok so this is probably a very beginner question but... Right now I have
This is probably an easy question, but hey, i'm a beginner. So I've created
I have this homework to do in C. I'm beginner so it is probably
Probably a very trivial problem. I have an object that looks like this: @PersistenceCapable
Probably something simple, but heres my problem, I have implememnted a simple captcha into
OK - I'll admit, I'm quite a beginner in this jQuery-department. I've probably made
Got a slight problem trying to have jquery UI and knockout js to cohoperate.
Beginner here! Forgive me in advance for raising what is probably an incredibly simple

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.