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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:16:46+00:00 2026-05-26T18:16:46+00:00

Let’s say I have a div with two anchors: <div id=#dialog> <a href=# class=ok>Delete</a>

  • 0

Let’s say I have a div with two anchors:

<div id="#dialog">
    <a href="#" class="ok">Delete</a>
    <a href="#" class="cancel">Cancel</a>
</div>

In order to make the <a> anchors work, I’m using the following two calls:

$('#dialog a.ok').click(function() {
    $.ajax({ ... });
    $(this).closest('#dialog').dialog('close');
});

 $('#dialog a.cancel').click(function() {
    $(this).closest('#dialog').dialog('close');
});

I’d like to be able to combine it to something like this instead:

$('#dialog').execute(function() {
    var dialog = $(this);

    dialog.find('a.ok').click(function() {
        $.ajax({ ... });
        dialog.dialog('close');
    });

    dialog.find('a.cancel').click(function() {
        dialog.dialog('close');
    });
});

The fake exeucte() function will execute the anonymous function immediately, without binding it to a DOM event. I’d like to use this pattern in order to group related functions together (in this case, a dialog has two buttons that are very closely related to each other).

My question is twofold:

  1. What is the actual jQuery call I should be using here?
  2. Is this the best way of grouping related functions together?
  • 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-26T18:16:47+00:00Added an answer on May 26, 2026 at 6:16 pm

    You don’t need any special jQuery stuff for this, just write a function and give it $('#dialog') as an argument:

    function do_stuff(dialog) {
        dialog.find('a.ok').click(function() {
            $.ajax({ ... });
            dialog.dialog('close');
        });
    
        dialog.find('a.cancel').click(function() {
            dialog.dialog('close');
        });
    }
    
    do_stuff($('#dialog'));
    

    Or, if you really have a thing for anonymous functions, you could use each:

    $('#dialog').each(function() {
        var dialog = $(this);
    
        dialog.find('a.ok').click(function() {
            $.ajax({ ... });
            dialog.dialog('close');
        });
    
        dialog.find('a.cancel').click(function() {
            dialog.dialog('close');
        });
    });
    

    but using each with a selector that can only match a single element looks funny. You could also define your own self-executing function:

    (function(dialog) {
        dialog.find('a.ok').click(function() {
            $.ajax({ ... });
            dialog.dialog('close');
        });
    
        dialog.find('a.cancel').click(function() {
            dialog.dialog('close');
        });
    })($('#dialog'));
    

    There is no best here, you use what fits your particular circumstances and local conventions.

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

Sidebar

Related Questions

Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I have a link in a table like: <td class=ms-vb width=100%> <a
Let's say I have a class like this: public class Person { private String
Let's say I have two files.. I want to compare them side-by-side and see
Let's say I have two entities: Physician Credentials And a physician can have many
Let's say I have the following entity: public class Store { public List<Product> Products
Let us assume I have two classes: class Base{}; class Derived: public Base{}; none
Let say I have two UIViews: View1: - bounds: 0, 0, 20, 20 -
Let's say I have two assemblies: BusinessLogic and Web. BusinessLogic has an application setting
Let's say I have two Databases like so : DatabaseA create table Table1 (

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.