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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:41:28+00:00 2026-06-11T23:41:28+00:00

My problem is that I have to create event listeners on certain DOM elements

  • 0

My problem is that I have to create event listeners on certain DOM elements before I insert them into the dom.

Now generally this is quite easy like so:

var button = $('<button>Click Me</button>');

button.on('click', function(){ alert('clicked'); });

$('body').append(button);

However, the problem is that I need to wrap the button in other html as in this example:

http://jsfiddle.net/a8xjJ/

The Event Listener breaks if I mix the button object with a string of html.
That’s when the Event Listener looses touch with the object, quite logically.

My problem is that I need to use strings of html, that’s a constraint of the project. Maybe there is a way in which I can use the dom selector on a string? Or maybe you can think of another way of making the above fiddle work.

  • 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-06-11T23:41:29+00:00Added an answer on June 11, 2026 at 11:41 pm

    If you started to work with objects, try not to use their string representation (it outputs the shell only but not the events and other data). One possible solution of you problem:

    $("<button>Click Me</button>").on("click", function(e) {
        e.preventDefault();
        alert("Clicked");
    }).wrap("<form><div></div></form>").closest("form").appendTo("div");
    

    DEMO: http://jsfiddle.net/a8xjJ/2/

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

Sidebar

Related Questions

The problem is the folowing: I have to create a panel in Vaadin that
I have a problem to create a preprocessor macro function, that concatenates two Strings
i have the following problem: i would like to create a footer. that footer
I need to use something like get_or_create() but the problem is that I have
I have defined the following ApplicationManifest file the problem is that i have created
I have created a dialog and got a problem that only part of the
i have created one image but problem is that when i save image from
I have created a street view backbone view. The problem is that when shown,
I have a problem regarding Android App. I have created an application that download
The problem is, I have a DLL and TLB that I created in C#.NET,

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.