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

The Archive Base Latest Questions

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

The first .click function is used to add an element(div) in a container and

  • 0

The first .click function is used to add an element(div) in a container and the second one is used to remove it from the container. The container initially has no elements. The removing of the function by clicking on it is not working

$(document).ready(function(){
    $(".class1").click(function(){
       //Code for adding element to the container and 
       // removing class1 from it and adding class2

    });

    $(".class2").click(function(){
       alert("hi");   //Even the alert is not displayed
       $(this).fadeOut(100);    
    });
});

However, it works if the element is already there before the page loads in the container.
Any reasons why? Is it because of document.ready function? Solutions?

  • 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-06T04:21:43+00:00Added an answer on June 6, 2026 at 4:21 am

    That’s because when you’re adding the click handler for .class2 elements, you’re only adding the event to elements which have that class at that specific moment in time; e.g none.

    Instead, you need to use event delegation like so;

    $(document).on('click', '.class2', function () {
        alert('hi');
        $(this).fadeOut(100);
    });
    

    This will work as it binds an event to document (which always exists), which listens for clicks on any .class2 elements using event delegation. For more info, read the on() documentation.

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

Sidebar

Related Questions

$(a.close).click(function() { var id = $(this).attr(id); alert(id); $(this).parents(div.venue:first).fadeOut(Fast); return false; }); Any ideas why
I change some methods from $(selector).live('click', function(){}); to $(selector).on('click', function(){}); . At first it
Here is my jquery code: $('input').click(function(){ $('h1').empty().queue(function(){ console.log('queue'); }); }); }); Only the first
Assuming I have the following two JQuery functions - The first, which works: $(#myLink_931).click(function
In order to add events we could use this simple first solution : function
$('.tabbed-block .tab-content:first').show(); $('.tabbed-block ol li:first').addClass('active'); $('.tabbed-block ol li a').click(function () { $('.tabbed-block ol li').removeClass('active');
var filters_used = new Array(); $('#filter-text').keyup(function(k) { $('#error').html(''); if (k.keyCode == 13) { $('#add-filter').click();
The following java-script is working fine (Jquery) $(document).ready(function(){ $('#c_area').load($('.m_top:first').attr('href')) }); $('.m_top').click( function(){ $(#myDiv).html('<img src=images/loading.gif
Please visit this link Click on first Image. Now is showing the Modal page
My Requiremet is when i Click on first button ,my panel should me me

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.