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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:35:05+00:00 2026-06-02T23:35:05+00:00

I have the following div: <div id=foo data-callback=function(){console.log(1)}></div> I want to be able to

  • 0

I have the following div:

<div id="foo" data-callback="function(){console.log(1)}"></div>

I want to be able to execute the div’s callback string as a JavaScipt function like this:

($('#foo').data('callback'))()

But this obviously won’t work. Is there any way to do this?

Here’s a JSFiddle.

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

    I suggest not storing code as a data- attribute. Make a function, then just store the function name as the attribute.

    <script>
    function callbackA(){
        console.log(1)
    }
    </script>
    <div id="foo" data-callback="callbackA"></div>
    

    Then you can do window[$('#foo').data('callback')]().

    EDIT: If you don’t want to do this, you can also make use of the onclick event (or any (built-in) event for that matter).

    <div id="foo" onclick="console.log(1)"></div>
    

    Then you can use .prop to get the event (as a function), and then use it.

    var callback = $('#foo').prop('onclick'); // get function
    $('#foo').removeProp('onclick'); // remove event
    callback();  // use function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: HTML : <div id='example'> <a href='#' data-name='foo' class='example-link'>Click Me</a>
I have the following code: HTML <div id='foo'> <input type='checkbox' value='english' name='bar[english]' /> <input
Suppose I have the following HTML: <div id=test> <span style=display:inline;>foo</span> <span style=display:none;>bar</span> <span style=display:inline;>baz</span>
Assume I have the following CSS: div { -my-foo: 42; } Can I later
I have the following html code: <div class=outer ui-draggable style=position: relative;> <div class=inner>Foo bar</div>
I have HTML similar to the following in my page <div id=someDiv> <img src=foo.gif
I have the following HTML: <DIV class=foo bar></DIV> I'm trying to create a CSS
I have the following HTML: <div> <p>foo</p> <p class='foo'>foo</p> <p class='foo'>foo</p> <p>bar</p> </div> How
Assuming I have the following html div which I want to replicate its children,
I have the following HTML node structure: <div id=foo> <div id=bar></div> <div id=baz> <div

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.