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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:05:00+00:00 2026-06-07T16:05:00+00:00

I have a snip of code that I cannot seem to fix. Everything works

  • 0

I have a snip of code that I cannot seem to fix. Everything works fine if I do not iterate through the function, or if I do not clone. When I put them together, the structure of the divs gets scrambled.

Before any code, I start with:

<div class="origin"></div>

Then I write some script to add html, which should be structured like this:
Code:

<div id="id0">
  <div class="scroller">
    <div class="origin"></div>
  </div>
  <a href="#"></a>
</div>
<div class="viewport"></div>
<div id="id1">
  <div class="scroller">
    <div class="origin"></div>
  </div>
  <a href="#"></a>
</div>
<div class="viewport"></div>

It starts fine, but if you uncomment the two lines below, the second div with ID is now inside the previous viewport, instead of wrapped around “origin”. I’m trying to copy the images and maybe captions, inside each “origin” to inside its own viewport (with two surrounding divs on the images).

The code:

function writeGallery(i) {
    var startDiv = $("div.origin:eq("+i+")");
    startDiv.wrap('<div class="scroller"></div>');  
    startDiv.parent().wrap ('<div id="id' + i + '" />');
    var horizThumbwrap      = startDiv.parent().parent();
    horizThumbwrap.append ('<a href="#"></a>');
    horizThumbwrap.after ('<div class="viewport" />');

    //var horizViewport        = startDiv.parent().parent().next(".viewport");
    //startDiv.clone().appendTo(horizViewport);
}

$(document).ready(function(){

$("div.origin").each(function(index) {
writeGallery (index);
});

$('#result').text($('#container').html());
});

The code here in fiddle (uncomment 2 lines to see mess): http://jsfiddle.net/AjSnx/

The only thing I can think of is to rewrite it in various ways and just hope I hit on something that works. But since it’s my first jQuery project, I’d like to understand what I’ve done wrong here, too.

Can anyone help?
Thanks!

  • 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-07T16:05:03+00:00Added an answer on June 7, 2026 at 4:05 pm

    Inside each loop, you are selecting the .origin elements using :eq(). However, in each loop you create another <div class="origin" />. So in the following loop you are selecting the <div> that you just created using .clone() and not the <div> that was there from the beginning. To fix this, use the second argument passed to the function that is called by .each(). It contains the current element that was matched in the beginning (i.e. the element you intended to use inside the loop).

    Please see my update for details. The HTML produced is this:

    <div id="id0">
        <div class="scroller">
            <div class="origin"></div>
        </div>
        <a href="#"></a>
    </div>
    <div class="viewport">
        <div class="origin"></div>
    </div>
    
    <div id="id1">
        <div class="scroller">
            <div class="origin"></div>
        </div>
        <a href="#"></a>
    </div>
    <div class="viewport">
        <div class="origin"></div>
    </div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have pasted a code snip below that I have written which reads a
I have a flash movie that dynamically generates urls based on some params. Snip:
We have an older C++ tool that generates some python code automatically. I tried
I have digging through the C# code generated by SWIG for Quantlib and came
I have two methods in C# 3.5 that are identical bar one function call,
While maintaining some code, I discovered that we have an infinite hang-up in a
I have the following function that draws some data from a chi-squared distribution and
Given the following code (that doesn't work): while True: # Snip: print out current
I have the following code snip and am trying to debug it. Is there
Looking back over my code I find that I occasionaly have written: ResultSet rs

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.