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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:49:02+00:00 2026-05-25T06:49:02+00:00

I have an array of jQuery objects that I built and which have events

  • 0

I have an array of jQuery objects that I built and which have events attached to them.

var obs = [];
for(var i=0;i<100;i++) {
  var j = jQuery('<div></div>');
  j.click(function() { console.log('Clicked ' + i); });
  obs.push(j);
}

My HTML is:

<div id="replaceme"></div>

How do I replace the div on the page with all the jQuery objects in my array? I don’t want to concatenate their HTML because I want to preserve the event handlers.

  • 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-25T06:49:02+00:00Added an answer on May 25, 2026 at 6:49 am

    You can replace an element with an array of jQuery elements by doing this:

    $(theElement).replaceWith(theArryaOfjQueryElements);
    

    In your case, this is:

    $('#replaceme').replaceWith(objs);
    

    See the jQuery.replaceWith() method.


    BTW you have an error here:

    j.click(function() { console.log('Clicked ' + i); });
    

    This will always print Clicked 100. The reason is that your function closes a reference to i, not to it’s value. So if i is modified before this function is called, the function will see the new value of i.

    You should do this instead:

     (function(i) {
         j.click(function() { console.log('Clicked ' + i); });
     }(i));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created an array of jquery objects that I want to hide when
I have simple AJAX function that uses jQuery to return an array of 300
I have an array of objects that can have up to 6 products in
In javascript, I have an array of objects that represents an arbitrarily deep list...
Given I have an array of purpose objects: //array of purpose objects: var purposeObjects
I have a jQuery post that returns some objects. So, I have a DB
I am trying to loop through an array of jquery objects I have created
I often have the need to search a javascript array that contains objects. I
I'm progamming in javascript/jQuery. Say, I have an array with includes a few number
I am trying to work out a complicated jQuery selection. I have an array,

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.