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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:38:58+00:00 2026-05-18T22:38:58+00:00

I have attached a onClick event handle to a tag. On click, I wanted

  • 0

I have attached a onClick event handle to a tag. On click, I wanted to pick-up an attribute — let’s say, fn; and I want to call the function mentioned as the attribute value.

The following code does not work

<html>
<head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
    <script language="JavaScript">
        var Test = Test? Test : new Object();
        $(document).ready(function(){
            Test.main();
        })
        Test.sub = function(){
            alert('called');
        }
        Test.main = function(){
            $('.caller').click(function(e){
                e.preventDefault();
                var fn = $(this).attr('fn');
                alert('calling: '+fn);
                return fn();
            });
        }
    </script>
</head>
<body>
    <a class="caller" fn="Test.sub" href="#">test call</a>
</body>
</html>

My questions are

  • Is it possible?
  • What am I doing wrong?
  • What could be right approach to solve this issue?

Thanks
Nishant

  • 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-18T22:38:59+00:00Added an answer on May 18, 2026 at 10:38 pm

    Is it possible?

    Yes. If you change your link to just have sub:

    <a class="caller" fn="sub" href="#">test call</a>
    

    Then this works:

    $('.caller').click(function(e){
        e.preventDefault();
        var fn = $(this).attr('fn');
        alert('calling: '+fn);
        return Test[fn]();
    });
    

    The value you retrieve is a string. You can index into an object’s properties using a string if you like using bracketed notation ([]), so all of these are equivalent:

    Test.sub();
    Test['sub']();
    var fn = 'sub';
    Test[fn]();
    

    What am I doing wrong?

    You were nearly there. You had “Test.sub” in a string, but as it was a string, not a function, you couldn’t call it. It would be like doing this:

    "Test.sub"();
    

    ….which obviously isn’t quite doing what you want. 🙂

    What could be right approach to solve this issue?

    In technical terms, the above solves the problem. Part of me worries about having the actual function name in an attribute and wants to say “have a lookup table instead”, but your Test object basically is the lookup table, so…

    Side note: In this situation, you may see people using eval. Usually that’s because they didn’t realize you could index into an object with bracketed notation. eval is best avoided.


    Off-topic #1: Your attribute, fn, is invalid (invalid = won’t validate). In HTML4 and earlier, all custom attributes were invalid, although every desktop browser I’ve ever seen allows them (browsers let us get away with lots of things). As of HTML5, the demand for this feature was heard and we can have valid custom attributes, but they must start with data- so the validator knows they’re custom. So if validation is part of your workflow (and it’s usually a good idea), you might go with data-fn="sub" rather than simply fn="sub". More here.


    Off-topic #2: If you want to save some typing, use {} rather than new Object(). E.g.:

    var Test = Test? Test : {};
    

    And if you want to save even more, use the curiously-powerful || operator:

    var Test = Test || {};
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have a regular onclick event attached to a few buttons, each function
I have a JToolTip attached to the onclick event of a JTextField. I want
I have an event handler attached to a class, as so: $('#container').on('click', '.myClass', function
I have a little javascript function which is attached to an onClick event of
I have a div which I have attached an onclick event to. in this
I have an onclick event attached to a button. Clicking the button adds a
I have a form on ASP .NET page, with button onclick event attached to
I have a LinkButton server control which have some attached method in OnClick Event
I have an onclick event attached to a region in my page that causes
I have the following code that wires up the click event function to the

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.