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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:24:18+00:00 2026-06-07T14:24:18+00:00

Here a small snippet to show/hide text. The problem is that the click event

  • 0

Here a small snippet to show/hide text. The problem is that the click event doesn’t fire for the “readless” class. Usually I would have use the “live” function of jQuery, but since its being deprecated in favor of “on”, I wonder how should I do it?

Here’s A jsfiddle:
http://jsfiddle.net/SSAu2/

code:

$(document).ready(function(){

    var showHiddenText = function(e){

        e.preventDefault();

        var $this = $(this);
        $this.prev().fadeIn();
        $this.text("less").removeClass("readmore-anchor").addClass("readless-anchor");
    };

    var hideShownText = function(e){

        e.preventDefault();

        var $this = $(this);
        $this.prev().fadeOut();
        $this.text("more").removeClass("readless-anchor").addClass("readmore-anchor");
    };

    $(".readmore").after("<a href='#' class='readmore-anchor'>More</a>");
    $(".readmore-anchor").on("click", showHiddenText);
    $(".readless-anchor").on("click", hideShownText);

});​
  • 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-07T14:24:21+00:00Added an answer on June 7, 2026 at 2:24 pm

    When the dom ready function executes, at the time there doesnt exist any element that matches the selector ‘.readless-anchor’. The way .on() works is, it needs one element to use as a parent. All events that occur under its descendants will bubble uptill the parent and will call the appropriate handler. Which is why its important that the primary selector for jquery already exist.

    From the documentation,

    Event handlers are bound only to the currently selected elements; they
    must exist on the page at the time your code makes the call to .on().

    The second parameter to the .on() function is the selector to filter with.

    In your case, you could anchor the .on() on body and filter by your classes.

    $("body").on("click", ".readmore-anchor", null, showHiddenText);
    $("body").on("click", ".readless-anchor", null, hideShownText);
    

    This is sort off equivalent to what you might have done using .live()

    $("body").find(".readmore-anchor").live('click', showHiddenText);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the problem -- I have a few thousand small text snippets, anywhere from
Here a small snippet of code that returns epsilon() for a real value: program
I recently did a small jQuery snippet that allows me to show a loading
I have a small code snippet for deleting element in linked list. Here is
Here a small piece of code for testing and explain the problem. I have
here is a small peice off code that i wrote. But I am not
Here is a small program showing a problem: import Data.Fixed main = do print
I'm having a small issue here: <div id=navbar> <ul id=navtabs class=floatcontainer> <li <?php if
I am stuck with a small problem here.. What i am trying to do
Here is a small snippet from a C# program to generate dictionary from file.

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.