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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:18:18+00:00 2026-05-28T16:18:18+00:00

So i have click function and i’m calling this function couple times on same

  • 0

So i have click function and i’m calling this function couple times on same div, so basicly i have this kind of div:

<div id="upload_submit" class="upload_submit">Content</div>

and i have two click functions:

    //first function
    $('.upload_submit').click(function(){
        $(this).removeClass('upload_submit');
        $(this).addClass('upload_cancel');
    });
    //second one
    $(".upload_cancel").click(function(){
        alert('test')
    });

So by default my div have class upload_submit after i click on that div i change class to upload_cancel so now if i click again i should get alert but for some reason it doesn’t happen, what could be wrong?

  • 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-28T16:18:19+00:00Added an answer on May 28, 2026 at 4:18 pm

    .bind() (which .click is a wrapper around) only binds to elements existing at the time of the call. But you are dynamically changing the class meaning that there is no .upload_cancel to bind to when you try to bind it. Therefore you need a delegated event:

    //first function
    $(document).delegate('.upload_submit', 'click', function(){
        $(this).removeClass('upload_submit');
        $(this).addClass('upload_cancel');
    });
    //second one
    $(document).delegate('.upload_cancel', 'click', function(){
        alert('test')
    });
    

    Or if you are using 1.7+ you can use the .on() syntax:

    //first function
    $(document).on('click', '.upload_submit', function(){
        $(this).removeClass('upload_submit');
        $(this).addClass('upload_cancel');
    });
    //second one
    $(document).on('click', '.upload_cancel', function(){
        alert('test')
    });
    

    What this will do instead is bind a handler to the document element for a click event. It will then check if the triggering element matches your selector you passed in, if it does it will call your handler. This means that you can add and remove elements matching the selector provided and it will still call your click event handler correctly since the event is bound to the document not your specific element.

    Here is a jsFiddle example of this solution.

    For more information you can start reading this article.

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

Sidebar

Related Questions

i have this code $(function() { $('#ans_vote a span').click(function(){alert('working');return false;});}); and this html <div
I have this jquery code : $(document).ready(function() { $('div.badge_button > .more').click(function() { var id
i have this click function note when clicked it should change a couple of
I have this piece of code: $(#faq).click(function () { var url = $.get(faq, {
I have this code: $('.user_info').click(function(){ var pos = $(this).offset(); rel_value = $(this).attr('rel'); $('#' +
I have a listener like this: $('.delete').click(function() { ...some stuff }); Also, on the
I have this code $(.delete2).click(function() { $('#load2').fadeIn(); } I have dynamically added item via
I have this (simplification): $(li).click(function{alert(test);}); <li> <input> </li> What's the best way to bind
Currently i have this: $(.splitCol).click(function () { $.cookie('whichColumn', 'split'); $(.threeCol .active).removeClass(active); $(.leftCol .active).removeClass(active); $(.splitCol
I have a click function setup to make a div resizable. When I click

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.