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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:10:43+00:00 2026-06-06T06:10:43+00:00

My problem is with correctly toggle -ing the state of some buttons. I use

  • 0

My problem is with correctly toggle-ing the state of some buttons. I use jquery click events. And I’m sorry for the long post, but I don’t know where the problem is and don’t want to accidentally miss it. I searched for similar questions but none seemed to fix my problem.

I start rendering a div containing some tasks I want to do for today. And I want to be able to toggle them as being done or not.

{
    $('#currentDiv').hide();
    rewriteDivFromScratch();
    $('#eachday').show();
}

Where the rewriteDivFromScratch adds button divs to HTML:

<div id="eachday"> .. </div>
    <div id="button-0" class="button"> <button class="btn btn-mini"> .. </button> </div>
    ..
    <div id="button-7" class="button"> <button class="btn btn-mini"> .. </button> </div>
function rewriteDivFromScratch() {
    el = $("#eachday");

    // get the HTML divs from Handlebars templating compilation
    // they're OK, I checked with inspector

    // This renderes nicely, no problem here.
    el.html(html);
}

I add a click event for buttons to toggle in the same file with those above:

$('.btn-mini').click( function(e) {
    e.preventDefault();
    var el = $(e.currentTarget);  // the <button />
    el.toggleClass('btn-inverse');
}

And I get no toggleing effect..

Now, I suppose this happens because there is not .btn-mini class by the time the $(..).click executes. This is why I added a setInterval like so:

var intervalId; // it's global in my js file
function rewriteDivFromScratch() {
    ...
    // This renderes nicely, no problem here.
    el.html(html);
    intervalId = setInterval(check); // this fires only once, so seems OK
}
var check = function() {
    $('.btn-mini').click( function(e) {
        e.preventDefault();
        var el = $(e.currentTarget);  // the <button />

        if (el.length)
            clearInterval(intervalId);
        else
            return;

        // It executes this toggle more than **15** times per
        // click on button.. Now, why is that?
        el.toggleClass('btn-inverse');
    }
};

As I’ve said in the above last comment, my problem is that that part gets executed multiple times, not a fixed number of times per refresh, in order to find a pattern or something.

The above js code is in a single ‘app.js’ which I simply include in my index.html like any other script.

  • 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-06T06:10:45+00:00Added an answer on June 6, 2026 at 6:10 am

    just use jquery’s ‘live’ for a ‘delegated event’, which will also work whenever new buttons are inserted into the document:

    $('.button').live('click', function(){
      //button clicked
    });
    

    see http://api.jquery.com/live/ for further explanations of how ‘live’ works.

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

Sidebar

Related Questions

Working on the N-queens problem. Having some difficult filling up the stack correctly. Was
My problem seemed easy at first but i got stuck. I have some containers
i have problem to correctly bind data to WPF Chart. When i'm setting ItemsSource
I'm having a bit of a problem using FieldOffset correctly with arrays. The code
Making websites that appear correctly in IE is a big problem. Is there any
I recently encountered a problem with my Profile provider: it wouldn't retrieve profiles correctly
Having a problem deploying an MVC application. Basically the site loads correctly, the home
Tomcat does not encode correctly String literals that contain unicode characters. The problem occurs
I have a problem in a custom view. Its state is not saved after
I have a custom toolbar that fires events on html button click, there is

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.