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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:34:21+00:00 2026-06-09T05:34:21+00:00

I am creating a few DOM elements dynamically like, var anchorElement = jQuery(‘<a />’,{text:property.text});

  • 0

I am creating a few DOM elements dynamically like,

var anchorElement = jQuery('<a />',{text:property.text});
var liElement = jQuery('<li />',{"class":"navlink_"+i,id:"navlink_"+i});
anchorElement.on('click',property.fnctn);
liElement.append(anchorElement);
parentID.append(liElement);

Where property is a JSON object.
property.text is the text that I want to put into anchor element. (Works fine)

I want to attach a click event handler to that anchor element.
The function that needs to be bound to that element is specified in JSON and we can access it like

property.fnctn

The following line should bind the event handler to the anchor element.

anchorElement.on('click',property.fnctn);

This was not working so I tried converting it into string like,

anchorElement.on('click',property.fnctn.toString());

No Success…

When I click on this link, the error is logged in the console

The object has no method ‘apply’.
What is the reason…???

I am able to get it working with a slight work around like

anchorElement.attr('onclick',property.fnctn+"()");

Above statement works, but I want to know why .on() API is not working.

Thanks 🙂
AÐitya.

  • 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-09T05:34:22+00:00Added an answer on June 9, 2026 at 5:34 am

    Update:

    Youve said that property.actfn is a string, "paySomeoneClick". It’s best not to use strings for event handlers, use functions instead. If you want the function paySomeoneClick, defined in the string, to be called, and if that function is global, you can do this:

    anchorElement.on('click',function(event) {
        return window[property.fnctn](event);
    });
    

    That works because global functions are properties of the global object, which is available via window on browsers, and because of the bracketed notation described below.

    If the function is on an object you have a reference to, then:

    anchorElement.on('click',function(event) {
        return theObject[property.fnctn](event);
    });
    

    That works because in JavaScript, you can access properties of objects in two ways: Dotted notation with a literal property name (foo.bar accesses the bar propety on foo) and bracketed notation with a string property name (foo["bar"]). They’re equivalent, except of course in the bracketed notation, the string can be the result of an expression, including coming from a property value like property.fnctn.

    But I would recommend stepping back and refactoring a bit so you’re not passing function names around in strings. Sometimes it’s the right answer, but in my experience, not often. 🙂

    Original answer:

    (This assumed that property.fnctn was a function, not a string. But may be of some use to someone…)

    The code

    anchorElement.on('click',property.fnctn);
    

    will attach the function to the event, but during the call to the function, this will refer to the DOM element, not to your property object.

    To get around that, use jQuery’s $.proxy:

    anchorElement.on('click',$.proxy(property.fnctn, property));
    

    …or ES5’s Function#bind:

    anchorElement.on('click',property.fnctn.bind(property));
    

    …or a closure:

    anchorElement.on('click',function(event) {
        return property.fnctn(event);
    });
    

    More reading (on my blog):

    • Mythical methods
    • You must remember this
    • Closures are not complicated
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating few elements with jQuery like this - var div1 = $('<div />',
I am creating a few pictureboxes dynamically, then assigning the following: // class variable
Relatively quick one; I'm creating a few elements dynamically, and am appending them to
I tried creating a disembodied DOM element with jQuery (I avoid ever touching the
i am creating a few appwidgets on android. I would also like to resuse
I've been creating a few websites with content being pulled with jQuery and CSS
I am creating a few user controls that will inherit from my class, BaseControl,
I am creating few pages (including login page) which should only be accessible by
i am creating a few divs from some JSONP data that i am retrieving
I'm creating quite a few SSIS packages to drive document composition (e.g. utility bills,

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.