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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:42:56+00:00 2026-06-09T22:42:56+00:00

Here is a simple piece of code: HTML: <div id=close>Click me</div> JS Code: $(#close).click(function(){

  • 0

Here is a simple piece of code:

HTML:

<div id="close">Click me</div>

JS Code:

$("#close").click(function(){
    alert("1");
    $(this).attr('id','expand');
});



$("#expand").live('click', function(){
    alert("2");
    $(this).attr('id','close');            
});

Problem: When I click on “close” it automatically calls live() too. See this in jsfiddle: http://jsfiddle.net/uFJkg/

Is it because I am changing the same element’s id from “close” to “expand”? How do I fix this problem?

I have tried:

 $("#expand").die().live('click', function()

but it didn’t work. I tried event.stopPropagation() but that didn’t help too. I tried having a separate div with id “expand” and hide it on document.ready but for some reason it is not working too.

Then I have tried since I read that live() has been deprecated in jQuery 1.7+:

$("#expand").on('click', function(){

and that is not working too.

Any thoughts? I think a fresh set of eyes will be able to spot what I am missing.

Thanks.

  • 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-09T22:42:57+00:00Added an answer on June 9, 2026 at 10:42 pm

    You need both delegate event (aka live) handler in this case. Because, you’re toggling between both ids and this toggling make both ids as dynamic to DOM.

    $("body").on("click", "#close", function(){
        alert("1");
        $(this).attr('id','expand');
    });
    
    $("body").on("click", "#expand", function(){
        alert("2");
        $(this).attr('id','close');            
    });
    

    DEMO

    Note

    As live() is deprecated, so instead of live() use .on() for delegate event handling.

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

Sidebar

Related Questions

I have this simple html piece of code, whenever I click on any of
Here I provide simple piece of code. function GetStringList:TStringList; var i:integer; begin Result:=TStringList.Create; Result.Add('Adam');
Here is a simple piece of code: import java.io.*; public class Read { public
my sample code is here include 'simple_html_dom.php'; function get_all_links($url){ global $host; $html = new
See this simple piece of code in PHP: //Documentation: //memcache_set ( string $key ,
Here's the code the piece of theory is about $.fn greenify = function(){ return
Here a simple C# piece of code: Convert.ToInt32(TimeSpan.FromMinutes(5).TotalMilliseconds); //which brings me 300000 (int)TimeSpan.FromMinutes(5).Milliseconds; //which
Here is a piece of code in edit.html.erb which does not work. The purpose
I have simple piece of code to upload files: $(document).ready(function () { $(.attachmentsUpload input.file).change(function
here my simple code to add filename and their associated icon to virtualtreeview PFileInfoRec

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.