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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:31:09+00:00 2026-06-08T16:31:09+00:00

I am attempting to attach an event handler to a dynamically created DOM element

  • 0

I am attempting to attach an event handler to a dynamically created DOM element in the following way:

var createdElement = $('<a>', {
    click: function (data) {
        alert("click");
    }
});

This works as expected, but now I want to pass a variable to the click function. Can anyone suggests how to do this.

  • 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-08T16:31:10+00:00Added an answer on June 8, 2026 at 4:31 pm

    Thecodeparadox’s answer works in the general case, but always passes the same value to the click handler, regardless of the <a> element on which it was triggered.

    If you want the value to depend on the clicked element, you can call on() on the element itself:

    var createdElement = $("<a>").on("click", {
        yourData: "something"
    }, function(event) {
        alert("Data is " + event.data.yourData);
    }).appendTo("body");
    

    However, the code above uses a different syntax than the one in your question. If you want to keep defining the click handler in a property/event map, you can use data() to associate a value with the element once and for all:

    var createdElement = $("<a>", {
        click: function() {
            alert("Data is " + $(this).data("yourData"));
        }
    }).data("yourData", "something").appendTo("body");
    

    (Note that I added calls to appendTo("body") so the new element becomes part of the DOM and the click event gets a chance to be triggered. Feel free to ignore this if you’re adding the element to the DOM later in your code.)

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

Sidebar

Related Questions

Attempting to get Spring internationalization working. I have used classpath:messages basename, created .properties files
Attempting to use the data series from this example no longer passes the JSONLint
I'm attempting to attach a file to a comment in a message with the
I am attempting to write a C# app that will attach to the 2008
How do I attach an onclick event to a link_to_function such that clicking on
I am attempting to apply a function to a dataset for each combination of
I am attempting to create a data set from an original dataframe (in either
I am attempting to dynamically generate cube-maps in OpenGL using a multi-pass rendering system.
I am new to LINQ to SQL and attempting to create a generic Data
I'm attempting the surprisingly difficult task of finding out which element was clicked. I

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.