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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:52:43+00:00 2026-05-28T01:52:43+00:00

I hvae the following code: $(.openDialog).live(click, function (e) { e.preventDefault(); $(<div></div>) .addClass(dialog) .attr(id, $(this).attr(data-dialog-id))

  • 0

I hvae the following code:

$(".openDialog").live("click", function (e) {
    e.preventDefault();

    $("<div></div>")
    .addClass("dialog")
    .attr("id", $(this).attr("data-dialog-id"))
    .appendTo("body")
    .dialog({
        title: $(this).attr("data-dialog-title"),
        close: function () { $(this).remove() },
        modal: true
    })
    .load(this.href);
});
$(".close").live("click", function (e) {
    e.preventDefault();
    $(this).closest(".dialog").dialog("close");
});

Can someone explain how I can decouple the functions form the actions? I am a bit confused with how to do this.

Also what is the purpose of “live” ? I heard before someone suggesting “on”. Is “on” better than “live” and how does it actually work?

  • 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-28T01:52:44+00:00Added an answer on May 28, 2026 at 1:52 am

    Just break the functions out, then pass the function name:

    close: closeFunction    
    
    function closeFunction() { 
       $(this).remove() 
    }
    

    $(".close").live("click", closeClick);
    
    function closeClick(e) {
        e.preventDefault();
        $(this).closest(".dialog").dialog("close");
    }
    

    on is indeed better than live. Both allow you to wire up events to dynamically added content, but the former is more efficient, and the latter is deprecated, . Here’s how you’d use it

    $(document).on("click", ".close", closeClick);
    

    or, ideally, if all these .close buttons were to be in a container, say, a div with id foo, you could more efficiently do

    $("#foo").on("click", ".close", closeClick);
    

    or for jQuery pre 1.7 you’d have to settle for delegate

    $("#foo").delegate(".close", "click", closeClick);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: window.requestFileSystem not working Following is the code to generate a csv file
I hvae the css located in app/Resources/FOSUserbundle/css How can i include that in my
i am on Linux VPS . i hvae installed softculous to install joomla using
I hvae a dropdownlist tied to a SQLDataSource that returns a list of Categories.
I'm implementing swipe gestures in my customtableviewcell control and so I hvae to implement
$(document).ready(function() alert($(td).val()); }); But the alert is empty. How do I select the values
I'm using Archiva as my repository manager. It is setup so the users hit
Ref URL; http://en.wikipedia.org/wiki/Future_value Earlier similar question; calculate Future value in iPhone Like we hvae
My tables: TableA (id number, state number) TableB (id number, tableAId number, state number)
I have an XSD file such as: <xs:schema targetNamespace=...> <xs: import namespace=...> <xs: import

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.