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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:26:03+00:00 2026-05-15T00:26:03+00:00

Outside of a for I declare a variable using var list; . I use

  • 0

Outside of a for I declare a variable using var list;. I use this variable inside of my for loop like so:

// add the html to the list
if (list == undefined)
    list = item;
else
    list.append(item.contents());

item is a cloned jquery object build from a $('list_template').clone(); call (list_template is a div with <li> elements inside). What I am doing is creating a list, which I will then appendTo() where I need it.

Right now this code works fine, but it doesn’t seem right to me. Unfortunatly, I cannot seem to figure out how to correctly declare the list variable to be an empty Jquery object. I have tried both:

var list = $([]);
var list = $('');

Both of those cause the append to not work correctly (or as expected) with list.html() being null. Is there a way to initialize my variable to an empty jquery object, so all I have to do is list.append(item.contents()); without the if/else statements?


Edit: Ok to lessen confusion here is the whole javascript function that currently works fine:

        var list;

        // Loop through all of the objects
        var objects = data.objects;
        for (x = 0; x < objects.length; x++) {
            // Clone the object list item template
            var item = $("#object_item_list_template").clone();

            // Setup the click action and inner text for the link tag in the template
            item.find('a').bind('click', { val: objects[x].Id }, function (e) { ShowObjectDetails(e.data.val); })
                          .html(objects[x].Name);

            // add the html to the list
            if (list == undefined)
                list = item;
            else
                list.append(item.contents());
        }
        // set the list of the topics to the topic list
        $("#object_list").empty();
        $('<ul>').appendTo("#object_list").append(list.contents());

The object list template is as follows:

<div id="object_item_list_template" style="display:none">
    <li class="object_item"><a href="#"></a></li>
</div>

This all works correctly by cloning the list item, setting up the click action, and adding it to the list on the display.

I am trying to get rid of the if/else statement. I can’t just do list.append() because if list is undefined (or not a Jquery object), it throws an exception.

  • 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-15T00:26:03+00:00Added an answer on May 15, 2026 at 12:26 am

    An empty jQuery object is declared as:

    $();
    

    Docs for jQuery object creation: http://api.jquery.com/jQuery/


    EDIT:

    It sounds like you’re trying to eliminate the if/else statement by extending list whether or not it has any content.

    Is that right?

    If so, try something like this:

    list = $( list.get().concat(item.get()) );
    

    or

    $.extend(list, item);
    

    (Assumes list is starting out as an empty jQuery object.)


    EDIT:

    Since you’re creating elements in a loop, you can always push() then into a jQuery object.

    Try something like this:

    var list = $();  // Start off with empty jQuery object.
    
    ...
    
    list.push(item.find('li').get(0));  // A jQuery object is an Array. You can `push()` new items (DOM elements) in.
    
    ...
    
    ('<ul>').appendTo("#object_list").append(list);
    

    (I edited from the original to only push the DOM element into the jQuery object.)

    Should add the current item in the loop to your list.

    You could eliminate the find() calls in your code if you just cloned the children of #object_item_list_template:

    $('li', '#object_item_list_template').clone();  // Clone the `li` without the `div`.
    

    Now you have a clone of the li itself. No need to do a find.

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

Sidebar

Ask A Question

Stats

  • Questions 420k
  • Answers 420k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can also use simple RS232 communications. I use always… May 15, 2026 at 10:45 am
  • Editorial Team
    Editorial Team added an answer $(".button").click(function() { alert($(this).attr("id")); }); May 15, 2026 at 10:45 am
  • Editorial Team
    Editorial Team added an answer Maybe the answer is there : https://bugs.java.com/bugdatabase/view_bug;jsessionid=36649bcae69963754fe1bd095030?bug_id=4671142 May 15, 2026 at 10:45 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.