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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:49:14+00:00 2026-05-16T08:49:14+00:00

Coming to jQuery from a functional background, I am fond (perhaps unreasonably so) of

  • 0

Coming to jQuery from a functional background, I am fond (perhaps unreasonably so) of elegant chains of functions. I often find myself dealing with arrays of elements, such as those that may result from $.map, and my ability to manipulate these arrays in the DOM seems quite limited. Here’s some sample code that runs through the results of a Google search, rendering the result titles:

var newResultsDiv = $('<div id="results" />');
$.each(searcher.results, function() {
  newResultsDiv.append('<p>' + this.title);
});
$("#searchresults").append(newResultsDiv);

I find this excessively verbose. Ideally, I would do something along these lines instead:

$.map(searcher.results, function(elem) {
  return $('<p>' + elem.title);
}).wrapAll('<div id="results" />').appendTo('#searchresults');

I’ve tried this out, along with several variants using different forms of append and wrap. They all appear to be incapable of handling the plain-old-Javascript array that jQuery.map spits out. They’re designed to work with jQuery’s own set collection. At least, that’s my guess, as messing around with these functions in the Firebug console seems to confirm the problem.

I am hoping that someone here has the wisdom to show me an elegant way to do what I’m trying to do. Is there one?

  • 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-16T08:49:14+00:00Added an answer on May 16, 2026 at 8:49 am

    Using the $.map method you presented, you could return the actual DOM element instead of a jQuery object. This is done by grabbing the [0] index item in the jQuery object. Then wrap the entire $.map with $().

    This works because jQuery will accept an array of DOM elements.

    Your <p> creation was a little off. I changed it to pass an object literal to set the text. Otherwise, you would need to concatenate the ending tag as well.

    Finally, you would need to traverse up to the wrapper #results you created using .parent().

    $($.map(searcher.results, function(elem) {
      return $('<p>',{text:elem.title})[0];
    })).wrapAll('<div id="results" />').parent().appendTo('#searchresults');
    

    EDIT: IF you don’t mind the look of it, you could do this as well:

    $('<div id="results" />').append(
        $.map(searcher.results, function(elem) {
            return $('<p>',{text:elem.title})[0];
        })).appendTo('#searchresults');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Coming from a PHP background, I'm used to writing small functions that return a
I'm coming from a Prototype background and looking into JQuery. I'd like to know
OK jQuery experts : So .. I'm coming from a Prototype background. I do
Firstly, I am new to jQuery, coming from a Java background, so apologies if
I am using jQuery to get data from server. Data is coming. Here what
Coming from a non-web background I'm struggling with cookie uniqueness. When I read and
Coming from a .NET background I'm use to reusing string variables for storage, so
I'm trying to parse some JSON coming from an AJAX request using jQuery. Basically,
I am trying to append li elements coming from a php file with JQuery.
This may be useful to see where I am coming from. Jquery nested each

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.