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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:44:14+00:00 2026-05-27T12:44:14+00:00

Maybe I’m just missing something simple, but it doesn’t seem like anyone else has

  • 0

Maybe I’m just missing something simple, but it doesn’t seem like anyone else has asked this question anywhere. I’m trying to create a new section of HTML nodes and would like to be able to do it all in a concise and chained manner.

Basically I’d like to do something like this:

var options = {
    buttons: {
        'New':  function() { /* Do some work*/ }
        'Edit': function() { /* Do some work*/ }
        // etc.
    }
};
$( '#container' ).append(
    $( '<div />', { 'class': 'table' } ).append(
        $( '<div />', { 'class': 'row' } ).append( function() {
            $.each(options.buttons, function(key, value) {
                $( '<button />', { text: key } ).click(value);
                // Somehow return this object so the append can work with it
            });
        })
    )
);

What I’d like to have happen is to have it so that the each function is able to return a button element with the click handler associated with it for each button in the options object. There are other options in for which I’d like to do the same thing, but I can’t figure out the syntax.

I know this can be done with the reverse having the $.each function contain the .append() function, but this seems to have a performance hit for long collections. Anyone have any suggestions?

Edit: I should note that I have tried the following as a work around, but I have to insert a new unneeded element.

$( '<div />', { 'class': 'row' } ).append( function() {
    var div = $( '<div />', { } );
    $.each(options.buttons, function(key, value) {
        div.append( $( '<button />', { text: key } ).click(value) );
    });
    return div;
})
  • 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-27T12:44:14+00:00Added an answer on May 27, 2026 at 12:44 pm

    Use $.map instead of $.each:

    return $.map(options.buttons, function(value, key) {
        return $( '<button />', { text: key } ).click(value)[0]; // return DOM node
    });
    

    JSFIDDLE DEMO


    Note that you can shorten it a bit if you get rid of the function of one of the appends:

    $( '#container' ).append(
        $( '<div />', { 'class': 'table' } ).append(
            $( '<div />', { 'class': 'row' } ).append( 
                $.map(options.buttons, function(value, key) {
                    return $( '<button />', { text: key } ).click(value)[0];
                })
            )
        )
    );
    

    JSFIDDLE DEMO

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

Sidebar

Related Questions

Maybe this is something I am just missing, but is there any way to
Maybe I am just forgetting something, but I cannot seem to get NUnit to
Maybe I'm missing something obvious, or maybe something sinister is happening, but this seems
Maybe I'm just thinking about this too hard, but I'm having a problem figuring
Maybe this is a simple question, but I didn't find answer in web. I
Maybe I'm having a brain fart or something because it seems like this should
Maybe it has been asked somewhere, but I am trying to find my question
Maybe this is gonna sound naive and all, but is there something even remotely
Maybe I'm missing something but why does visual studio break execution to tell me
maybe this might be a simple problem but after a long while at Google

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.