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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:29:14+00:00 2026-06-04T22:29:14+00:00

I am having a hard time getting some jQuery functions to work like it

  • 0

I am having a hard time getting some jQuery functions to work like it should.

I have a like button on my site that works until new items are appended, then it stop to work.
This worked when I had the following code:

$('.like').toggle(
    function() {
        console.log('href');
    }, function() {
        console.log('rel');
    }
);

A friend of mine directed me to event bubbling, but I am having a hard time getting it to work.

Here is what I have at the moment.

the simple HTML

​<button href='123.html' rel='456.html' class='like'>Click here</button>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

the jQuery, wrapped in $(document).ready( …
I have tried the toggle function, and it seems like the button is asleep for the first click, then it suddenly wakes up and performs the event.

$('body').click(function(event) {
    if ($(event.target).is('.like')) {
        var $like = $(event.target);
        $like.toggle(
            function() {
                console.log('href');
            }, function() {
                console.log('rel');
            }
        );
    }
});​

What should the code be like in order to keep working when new items are appended and make sure that the button does not act this way

Here is a fiddle.

http://jsfiddle.net/_entreprenerd/SpmbQ/

  • 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-04T22:29:16+00:00Added an answer on June 4, 2026 at 10:29 pm

    The thing is, we can’t delegate “toggle”, because toggle is not an event, it’s just a method in jQuery. So you need to implement your own toggle functionality for delegated events.

    Sample solution:

    $("body").on("click", ".like", function () {
        var $this = $(this);
        var toggled= $this.data("toggled");
        if (toggled) {
            console.log('rel');
        } else {
            console.log('href');
        }
        $this.data("toggled", !toggled);
    });
    

    I’d also suggest changing the selector in the above code from “body” to parent container of .like elements, so the delegation has shorter way up the document tree.

    and a sample fiddle: http://jsfiddle.net/t2pXr/3/

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

Sidebar

Related Questions

I am having a really hard time getting this to work. I have two
I'm having some hard time getting PHP APC to work. Here's my test code:
I am having hard time getting hasErrors to work with indexed properties. For example
I'm having a hard time getting my AJAX requests to work on a staging
I am having a hard time getting cxf to validate an xml request that
I'm having a hard time getting this to work, and I'm hopelessly confused with
i'm having a hard time getting this to work, all I need to do
I am new with jQuery and I'm having a hard time getting my page
I'm having a hard time getting some sql in python to correctly go through
I am having a hard time getting started with some way to visualize some

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.