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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:12:40+00:00 2026-05-27T05:12:40+00:00

Consider the HTML <ul> <li>Default item</li> <li>Default item</li> </ul> <button>Append</button> and the jQuery code

  • 0

Consider the HTML

<ul>
    <li>Default item</li>
    <li>Default item</li>
</ul>
<button>Append</button>

and the jQuery code

$('button').live('click', function(){  //This action is done by an external script.
    $('ul').append('<li>Added item</li>'); 
});

$('ul li').append('<b> x</b>'); // This action is done by me

The thing is, I need to append the “x” mark to all newly added elements to the dom.

In this case only default elements are appended with the “x” mark.

Newly added elements are not appended by “x”.

I am sure the work will be simple, but cant get it right !!

Live example – http://jsfiddle.net/vaakash/LxJ6f/1/

  • 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-27T05:12:41+00:00Added an answer on May 27, 2026 at 5:12 am

    Your code is running right away, and so of course it only has access to the elements that already exist. The code adding new list items is running later, when the user clicks something. You’ll have to hook into that process as well.

    One way is to hook the same event they are, and run your code from the event handler. Be sure to hook the event after they do.

    Their code:

    $('button').live('click', function(){
        $('ul').append('<li>Added item</li>');
    });
    

    Your code (after theirs):

    $('button').live('click', markButtons);
    
    markButtons();
    
    function markButtons() {
        $('ul li:not(.marked)')
            .addClass("marked")
            .append('<b> x</b>');
    }
    

    Updated fiddle

    The reason I said your code needs to do its hookup after their code is that jQuery guarantees the order of the calls to event handlers: When the event occurs, the handlers are called in the order in which they were attached. By attaching your handler after they attach theirs, you guarantee that your handler is called after theirs has done its thing.

    If you’re worried about the order getting mixed up, you could always delay your code slightly:

    $('button').live('click', function() {
        setTimeout(markButtons, 0);
    });
    

    That way, your code is guaranteed to run after all of the event handlers hooked to the click have been run.

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

Sidebar

Related Questions

consider this html code <textarea> <object> <embed></embed> <div></div </object> </textarea> If i issue the
Consider this code : HTML: <div class='a'>a</div> <div class='b'>b</div> <div id='log'></div> CSS: .a, .b
Consider this code : HTML: <table> <tr> <td>Option 1</td> <td><input type='checkbox' id='option1' /></td> </tr>
Consider the following html code: <div id='x'><div id='y'>Y content</div>X content</div> I'd like to extract
This problem is only in IE. Consider the following HTML: <html> <body> <div style='position:absolute;left:1em;right:1em;top:1em;bottom:1em;overflow:auto;>
Consider this page @ http://www.bloodbone.ws/screwed.html I need to be able to have the a.grow
Consider such html piece: <p>foo</p><p>bar</p> If you run (for example) jQuery text for it
Let's consider following html code: <p> Some text followed by <span>a span element</span> and
Consider this HTML: <form id=MyForm> <input name=Input1/> </form> In Script# I do this: FormElement
Consider this HTML: <form id=MyForm> <input name=Input1/> </form> To get the form, I would

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.