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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:33:06+00:00 2026-05-20T04:33:06+00:00

I have a <ul> with number of <li> s each having its click handler

  • 0

I have a <ul> with number of <li>s each having its click handler being attached on page load. There is a button to sort these <li>s. In my sort functionality I have sorted the array of jQuery elements of <li>s and then appended them into the <ul>. this way the sorting works fine but all the <li> lose their click handlers. How do i preserve them ?

var li_array = $('ul li');
li_array.sort( custom_func );
ul.empty().append( li_array.clone(true, true) );
  • 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-20T04:33:06+00:00Added an answer on May 20, 2026 at 4:33 am

    li_array.sort() should not work as li_array is not an array, but a jQuery object (unless you use a plugin). Update: It seems, jQuery does indeed provide a sort method and it is the same built-in function that arrays are using. This surprises me a little as I couldn’t find anything in the document. Does someone has more information about this?

    Furthermore, there is no need to clone the elements (which implies you don’t have to empty the list). If you append existing DOM elements to another element, jQuery takes care to detach it first.

    This should work (assuming ul references your list):

    var li_array = $('ul li').get(); // li_array will be an array of **DOM elements**
    li_array.sort( custom_func );
    $(li_array).appendTo(ul); 
    // or $(li_array).appendTo('ul'); or however you reference the list 
    

    If you attach the same click handler to each <li> element, you better make use of event delegation, using .delegate():

    $('ul').delegate('li', 'click', function() {
        // handler
    }); 
    

    More information about delegate:

    Event delegation makes use of the fact, that events are bubbling up the DOM tree. The code above adds a click event listener to the ul element. The handler will be triggered for every click event that is raised on descendants of this element. Now, jQuery is smart and it executes the handler only for li elements (elements that match the selector, passed as first argument to delegate.

    The advantage is that you don’t add event handlers to every li element, but just one to their common ancestor.
    You could also add more li elements to the list without adding the event listeners to them. The event handler bound to ul would also capture clicks on the newly added elements.

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

Sidebar

Related Questions

I have a number of check-boxes on my page, each has a name attribute
I have number of movieClips on stage, with each having it's own Event Listener.
In each table have many number of rows with amount. Like this i have
I have a number of models that store words as a string with each
I have a number of traceroutes that i need to compare against each other
Say I have a number of trait s that each add some behaviour to
I have a number of RGBA pixels, each of them has an alpha component.
I have a file with great amount of numbers,each number is separated by a
I have a number of objects, all from the same class(ColorNum) Each object has
If I have the following example file where each number is represents a byte

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.