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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:41:13+00:00 2026-06-06T09:41:13+00:00

I have a function that I use as a callback for a custom event:

  • 0

I have a function that I use as a callback for a custom event:

function checkNCreateTb() {
        var tbName = $(this).attr('href').replace('content','orders')
        /*...*/
}

I used .on() to handle the event that will callback that function

$('#group-pills a').on('shown', checkNCreateTb)

I need to apply the same function the first time the user clicks a button in another part of the DOM. I solved it attaching a click event to the button and triggering the shown event. However, I think this looks very ugly, Is there a better way to get the same result?

The code:

$('button#action-button').on('click',function() {
    $('#group-pills a:first').trigger('shown')
    $(this).off('shown') //removing the handler
})

//Create the DataTable if it wasn't already created
$('#group-pills a').on('shown', checkNCreateTb)
  • 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-06T09:41:15+00:00Added an answer on June 6, 2026 at 9:41 am

    I would use $.proxy to create a function reference with this set to a particular value:

    $("#action-button").on("click", $.proxy(checkNCreateTb, $("#group-pills a:first")));
    

    Or, alternatively, use call to set the value of this in checkNCreateTb:

    $('#action-button').on('click',function() {
        checkNCreateTb.call($("#group-pills a:first"));
    });
    

    As a side note, you do not need to prefix your #id selector with a tag name, since id is as specific as you can get and jQuery will use document.getElementbyId under the hood.

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

Sidebar

Related Questions

I have this very simple br2nl function that I use to take a string
I have a custom class that looks like: function myClass(){ var thing; var thingtype;
I have a simple function that uses a callback and I want use functor
I have a function that use setInterval() method to watch css propertie change('cause in
I have a function that I use on index.php page and I would like
I have a function that I use quite frequently, which allows me to write
I have a simple function that returns an NSString after decoding it. I use
I have a downloader function that downloads multiple files parallely. I use multiprocessing.Pool.map_async in
I have a C library function that expects a function pointer for callback, and
I have a callback function that I'd like to execute after 2 separate $.getScript

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.